简体   繁体   English

QT,C ++无法在没有对象的情况下调用成员函数

[英]QT, C++, cannot call member function without object

In my mainwindow.cpp is where call is made. 在我的mainwindow.cpp中是进行调用的地方。 Call is supposed to occur on button clicked event. 应该在按钮单击事件上发生呼叫。 Error says cannot call member function 'void Foo::setFooAttributes(A&, B&, C&, D&)' without object . 错误提示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);

}

The function itself looks like this: 该函数本身如下所示:

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();

}

I put the rest of code including foo.h in paste here pastebin source 我将包括foo.h在内的其余代码粘贴到此处pastebin源代码中

I first tried calling setFooAttributes(a,b,c,d); 我首先尝试调用setFooAttributes(a,b,c,d); without Foo:: but that gave me error like 'setFooAttributes' was not declared in this scope 没有Foo::但是给了我这样'setFooAttributes' was not declared in this scope错误,例如'setFooAttributes' was not declared in this scope

Make your Foo::setFooAttributes a static member function, since it doesn't operate on a "this" instance. 让你Foo::setFooAttributes一个static成员函数,因为它并不在“此”实例进行操作。

While you're at it, if A/B/C/D are actually the same type (or a subtype of a common type), you might consider removing all the duplication, to better follow the DRY (Don't Repeat Yourself) principle: 在使用它时,如果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);
}

(This would need you to convert each of {a,b,c,d}InterfaceStopPoint , {a,b,c,d}InterfaceRate , {a,b,c,d}InterfaceDomain and {a,b,c,d}InterfaceLength into arrays, instead of four individual variables). (这需要您转换{a,b,c,d}InterfaceStopPoint{a,b,c,d}InterfaceRate{a,b,c,d}InterfaceDomain{a,b,c,d}InterfaceLength进入数组,而不是四个单独的变量)。

You could even improve on this by using a loop or a variable-arity template function, but I'll leave that as an exercise. 你甚至可以通过使用循环或可变参数数量模板函数这一提高,但我会离开,作为一个练习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM