在我的mainwindow.cpp中是调用的地方。呼叫应该发生在点击按钮的事件上。错误说cannot call member function 'void Foo::setFooAttributes(A&, B&, C&, D&)' without object
。QT,C++不能在没有对象的情况下调用成员函数
void MainWindow::on_generateButton_clicked()
{
setCreator(ui->interfaceCreatorName->text());
setAlternateName(ui->interfaceAlternateName->text());
setDomain(ui->interfaceDomain->text().toInt());
QString filename = getCreator() + "'s " + getAlternateName() + ".txt";
QFile file(filename);
A a; B b; C c; D d;
Foo::setFooAttributes(a,b,c,d); //ERROR: cannot call member function without object
generateTop(file);
generateMiddle(file);
generateSecondaryMid(file);
generateLast(file);
generateTertiaryMid(file, a, b, c, d);
}
函数本身看起来像这样:
void Foo::setFooAttributes(A &aFoo, B &bFoo, C &cFoo, D &dFoo){
aFoo.stopPoint = MainWindow.ui->aInterfaceStopPoint->text().toDouble();
aFoo.rate = MainWindow.ui->aInterfaceRate->text().toInt();
aFoo.domain = MainWindow.ui->aInterfaceDomain->text().toInt();
aFoo.length = MainWindow.ui->aInterfaceLength->text().toInt();
bFoo.stopPoint = MainWindow.ui->bInterfaceStopPoint->text().toDouble();
bFoo.rate = MainWindow.ui->bInterfaceRate->text().toInt();
bFoo.domain = MainWindow.ui->bInterfaceDomain->text().toInt();
bFoo.length = MainWindow.ui->bInterfaceLength->text().toInt();
cFoo.stopPoint = MainWindow.ui->cInterfaceStopPoint->text().toDouble();
cFoo.rate = MainWindow.ui->cInterfaceRate->text().toInt();
cFoo.domain = MainWindow.ui->cInterfaceDomain->text().toInt();
cFoo.length = MainWindow.ui->cInterfaceLength->text().toInt();
dFoo.stopPoint = MainWindow.ui->dInterfaceStopPoint->text().toDouble();
dFoo.rate = MainWindow.ui->dInterfaceRate->text().toInt();
dFoo.domain = MainWindow.ui->dInterfaceDomain->text().toInt();
dFoo.length = MainWindow.ui->dInterfaceLength->text().toInt();
}
我把代码的其余部分,包括贴在这里pastebin source
我第一次尝试调用setFooAttributes(a,b,c,d);
没有Foo::
但是这给了我foo.h中错误如'setFooAttributes' was not declared in this scope
问题是关于基本的C++。您调用Foo :: setFooAttributes的方式仅适用于静态函数。否则,你需要做的是错误所说的 - 拥有Foo类的对象。 Foo f; f.setFooAttributes(); – mvidelgauz
嗨,ty快速回复。我想一次为多个对象设置属性,而不是a.setAttributes,b.setAttributes等。理想情况下,调用将是“setFooAttributes(a,b,c,d);”但编译器对范围持怀疑态度。我不知道如何解决范围问题,所以我如上所示 – Ranfan
您可以使该函数成为非类函数。只要将它移出课程并在没有'Foo ::'的情况下调用它。 – Bugfinger