[英]QT, C++, cannot call member function without object
在我的mainwindow.cpp中是进行调用的地方。 应该在按钮单击事件上发生呼叫。 错误提示cannot call member function 'void Foo::setFooAttributes(A&, B&, C&, D&)' without object
。
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();
}
我将包括foo.h在内的其余代码粘贴到此处pastebin源代码中
我首先尝试调用setFooAttributes(a,b,c,d);
没有Foo::
但是给了我这样'setFooAttributes' was not declared in this scope
错误,例如'setFooAttributes' was not declared in this scope
让你Foo::setFooAttributes
一个static
成员函数,因为它并不在“此”实例进行操作。
在使用它时,如果A / B / C / D实际上是同一类型(或常见类型的子类型),则可以考虑删除所有重复项,以便更好地遵循DRY(不要重复自己)原理:
template <typename A> static void Foo::setFooAttributes(Foo &aFoo, int iface) {
aFoo.stopPoint = MainWindow.ui->interfaceStopPoint[iface]->text().toDouble();
aFoo.rate = MainWindow.ui->interfaceRate[iface]->text().toInt();
aFoo.domain = MainWindow.ui->interfaceDomain[iface]->text().toInt();
aFoo.length = MainWindow.ui->interfaceLength[iface]->text().toInt();
}
static void Foo::setFooAttributes(Foo &aFoo, Foo &bFoo, Foo &cFoo, Foo &dFoo) {
setFooAttributes(aFoo, 0);
setFooAttributes(bFoo, 1);
setFooAttributes(cFoo, 2);
setFooAttributes(dFoo, 3);
}
(这需要您转换{a,b,c,d}InterfaceStopPoint
, {a,b,c,d}InterfaceRate
, {a,b,c,d}InterfaceDomain
和{a,b,c,d}InterfaceLength
进入数组,而不是四个单独的变量)。
你甚至可以通过使用循环或可变参数数量模板函数这一提高,但我会离开,作为一个练习。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.