关于友元类的问题~~

sanmu111111 2012-07-18 10:42:52
我声明了两个类,并且互相声明为友元类了,为什么我在B类中使用A类的成员时,会提示我:'lineedit31' was not declared n this scope

这是为什么呢~???
...全文
290 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiebin133 2012-07-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
引用 6 楼 的回复:

写公有的方法,比如getLabelShow(){return label;};之类的去访问。



这样也不行啊~~还是会提示getLabelShow()函数中的lineedit没有声明啊~~~
[/Quote]
假设你的类有一个 QLineEdit *m_lineEdit,然后其他的类要用到这个m_lineEdit,你可以在类中写一个方法,QLineEdit *getLineEdit(){return this->m_lineEdit;};
sanmu111111 2012-07-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

写公有的方法,比如getLabelShow(){return label;};之类的去访问。
[/Quote]


这样也不行啊~~还是会提示getLabelShow()函数中的lineedit没有声明啊~~~
sanmu111111 2012-07-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

写公有的方法,比如getLabelShow(){return label;};之类的去访问。
[/Quote]

这个成员函数的类型应该是什么呢??返回值是label的话~~~我没写类型,提示错误:ISO C++ forbids declaration of 'getlabelshow' whit no type
xiebin133 2012-07-19
  • 打赏
  • 举报
回复
写公有的方法,比如getLabelShow(){return label;};之类的去访问。
sanmu111111 2012-07-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

labelshow1 = new QLabel(lineEdit31->text()); //问题在这里~~
lineEdit31是ClassConfigPage的成员,不是ClassConfigShowPage的成员,当然不能这样用了。要想办法让ClassConfigShowPage访问到对应的ClassConfigPage对象。
[/Quote]

是啊~~我就是想让ClassConfigShowPage访问到对应的ClassConfigPage的成员,所以将这两个类互相设为友元类了,但是还是提示没有声明呢~~
windywater119 2012-07-19
  • 打赏
  • 举报
回复
labelshow1 = new QLabel(lineEdit31->text()); //问题在这里~~
lineEdit31是ClassConfigPage的成员,不是ClassConfigShowPage的成员,当然不能这样用了。要想办法让ClassConfigShowPage访问到对应的ClassConfigPage对象。
xiebin133 2012-07-19
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]
这样也不行啊,this指的是本类,他会提示本类中没有声明m_lineEdit这个变量啊~~

Qt里的友元的用法难道跟C++不一样吗,大神们帮帮忙啊~~
[/Quote]
你在ClassConfigPage 类中写个公有方法,QLineEdit *getLineEdit31(){return this->lineEdit31;};
然后你在ClassConfigShowPage 类里面定义一个ClassConfigPage 对象,用这个对象调用getLineEdit31()方法就可以得到了。
你不会希望他们互相调用对方的子控件吧?这样的方式不可取的,你把两个界面完全关联到一起来了。
sanmu111111 2012-07-19
  • 打赏
  • 举报
回复
这样也不行啊,this指的是本类,他会提示本类中没有声明m_lineEdit这个变量啊~~

Qt里的友元的用法难道跟C++不一样吗,大神们帮帮忙啊~~
sanmu111111 2012-07-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

代码贴出来看看
[/Quote]
//dialog.h

#ifndef DIALOG_H
#define DIALOG_H
#include <QWizard>
#include <QPainter>
#include <QtGui/QWidget>
#include <QFile>
#include <QTextStream>

class QCheckBox;
class QGroupBox;
class QLabel;
class QLineEdit;
class QRadioButton;

////////////////////////////////////////////////////////////////////////
class ClassWizard : public QWizard
{
Q_OBJECT

public:
ClassWizard(QWidget *parent = 0);

//void accept();
};

////////////////// ConfigPage ////////////////////////

class ClassConfigPage : public QWizardPage
{
Q_OBJECT

public:
ClassConfigPage(QWidget *parent = 0);
QLineEdit *lineEdit31; /////我要在ClassConfigShowPage里用这几个lineedit
QLineEdit *lineEdit32;
QLineEdit *lineEdit33;
QLineEdit *lineEdit34;
QLineEdit *lineEdit35;


private:


QLabel *labelconfig;
QLabel *label31;
QLabel *label32;
QLabel *label33;
QLabel *label34;
QLabel *label35;

QPushButton *OKButton;

private slots:
// void savedata();
void on_OKButton_clicked();

friend class ClassConfigShowPage;

};

///////////////// ConfigShowPage ////////////////////

class ClassConfigShowPage : public QWizardPage
{
Q_OBJECT

public:
ClassConfigShowPage(QWidget *parent = 0);

private:
QLabel *labelshow;
QLabel *config;
QLabel *labelshow1;


friend class ClassConfigPage;

};

#endif // DIALOG_H

//dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include <QtGui>
#include <QTextStream>

ClassWizard::ClassWizard(QWidget *parent)
: QWizard(parent)
{
addPage(new IntroPage);
addPage(new ClassInfoPage);
addPage(new ClassConfigPage);
addPage(new ClassConfigShowPage);
addPage(new ClassThroughputPage);


//setPixmap(QWizard::BannerPixmap, QPixmap(":/images/banner.jpg"));
// setPixmap(QWizard::BackgroundPixmap, QPixmap(":/images/background.jpg"));

setWindowTitle(tr("Class Wizard"));

}

ClassConfigPage::ClassConfigPage(QWidget *parent)
: QWizardPage(parent)
{
setTitle(tr("ConfigPage"));
// setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/background.jpg"));

labelconfig = new QLabel("ConfigPage");
labelconfig->setWordWrap(true);


label31 = new QLabel("UE Style(central;middle;edge):");
lineEdit31 = new QLineEdit;
label31->setBuddy(lineEdit31);
QHBoxLayout *layout31 = new QHBoxLayout;
layout31->addWidget(label31);
layout31->addWidget(lineEdit31);



label32 = new QLabel("Number of BS:");
lineEdit32 = new QLineEdit;
label32->setBuddy(lineEdit32);
QHBoxLayout *layout32 = new QHBoxLayout;
layout32->addWidget(label32);
layout32->addWidget(lineEdit32);



label33 = new QLabel("Number of UE:");
lineEdit33 = new QLineEdit;
label33->setBuddy(lineEdit33);
QHBoxLayout *layout33 = new QHBoxLayout;
layout33->addWidget(label33);
layout33->addWidget(lineEdit33);



label34 = new QLabel("...:");
lineEdit34 = new QLineEdit;
label34->setBuddy(lineEdit34);
QHBoxLayout *layout34 = new QHBoxLayout;
layout34->addWidget(label34);
layout34->addWidget(lineEdit34);



label35 = new QLabel("...:");
lineEdit35 = new QLineEdit;
label35->setBuddy(lineEdit35);
QHBoxLayout *layout35 = new QHBoxLayout;
layout35->addWidget(label35);
layout35->addWidget(lineEdit35);

/////////////////////////////////////////////////////////////////

QPushButton *OKButton = new QPushButton("OK");

QVBoxLayout *leftlayout = new QVBoxLayout;
leftlayout->addLayout(layout31);
leftlayout->addLayout(layout32);
leftlayout->addLayout(layout33);
leftlayout->addLayout(layout34);
leftlayout->addLayout(layout35);

QHBoxLayout *downlayout = new QHBoxLayout;
downlayout->addLayout(leftlayout);
downlayout->addWidget(OKButton);

QVBoxLayout *layout3 = new QVBoxLayout;
layout3->addWidget(labelconfig);
layout3->addLayout(downlayout);
setLayout(layout3);


QObject::connect(OKButton, SIGNAL(clicked()), this, SLOT(on_OKButton_clicked()));


}

void ClassConfigPage::on_OKButton_clicked()
{
QFile file("F:/QtWorkshop/test_next3/123.txt");
file.open(QIODevice::ReadWrite|QIODevice::Text);
QTextStream in1(&file);
//in1 << "sdkfjioweu" <<endl;
in1 << "UE Style(central;middle;edge):" << lineEdit31->text() << endl;
QTextStream in2(&file);
in2 << "Number of BS:" << lineEdit32->text() << endl;
QTextStream in3(&file);
in3 << "Number of UE:" << lineEdit33->text() << endl;
QTextStream in4(&file);
in4 << "..." << lineEdit34->text() << endl;
QTextStream in5(&file);
in5 << "..." << lineEdit35->text() << endl;
}


ClassConfigShowPage::ClassConfigShowPage(QWidget *parent)
: QWizardPage(parent)
{
setTitle(tr("ConfigShowPage"));
// setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/background.jpg"));

labelshow = new QLabel(tr("ConfigShowPage"));
labelshow->setWordWrap(true);

QFile file("F:/QtWorkshop/test_next3/123.txt");
file.open(QIODevice::ReadOnly);
QTextStream fs(&file);
QString filecontent=fs.readAll();

config = new QLabel(filecontent);
config->setText(filecontent);

labelshow1 = new QLabel(lineEdit31->text()); //问题在这里~~

QVBoxLayout *layout4 = new QVBoxLayout;
layout4->addWidget(labelshow);
layout4->addWidget(config);
layout4->addWidget(labelshow1);
setLayout(layout4);
}

sanmu111111 2012-07-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

代码贴出来看看
[/Quote]

哎,好吧,以为说说就能解决了呢~~有点多,贴出来看看吧~~呵呵
windywater119 2012-07-18
  • 打赏
  • 举报
回复
代码贴出来看看

16,173

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧