Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QT,C++;,无法在没有对象的情况下调用成员函数_C++_Qt_Oop - Fatal编程技术网

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

C++ QT,C++;,无法在没有对象的情况下调用成员函数,c++,qt,oop,C++,Qt,Oop,在my Main Window.cpp中进行呼叫。调用应该发生在单击按钮的事件上。错误说明无法在没有对象的情况下调用成员函数“void Foo::setfootattributes(A&,B&,C&,D&)” void MainWindow::on_generateButton_clicked() { setCreator(ui->interfaceCreatorName->text()); setAlternateName(ui->interfaceAlternat

在my Main Window.cpp中进行呼叫。调用应该发生在单击按钮的事件上。错误说明
无法在没有对象的情况下调用成员函数“void Foo::setfootattributes(A&,B&,C&,D&)”

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在内的其余代码粘贴到这里


我第一次尝试调用
setfootattributes(a、b、c、d)
没有
Foo::
但这给了我一个错误,比如
'setfootattributes'没有在这个范围内声明

使您的
Foo::setfootattributes
成为
静态的
成员函数,因为它不在“this”实例上运行

当你在它的时候,如果A/B/C/D实际上是同一类型(或者一个普通类型的子类型),你可以考虑删除所有的重复,以更好地遵循干(不重复你)的原则:

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);
}
模板静态void Foo::setfootattributes(Foo&aFoo,int-iface){
aFoo.stopPoint=mainfown.ui->interfaceStopPoint[iface]->text().toDouble();
aFoo.rate=MainWindow.ui->interfaceRate[iface]->text().toInt();
aFoo.domain=MainWindow.ui->InterfacedDomain[iface]->text().toInt();
aFoo.length=MainWindow.ui->interfaceLength[iface]->text().toInt();
}
静态void Foo::setfootattributes(Foo&aFoo、Foo&bFoo、Foo&cFoo、Foo&dFoo){
setFooAttributes(aFoo,0);
setfootattributes(bFoo,1);
setfootattributes(cFoo,2);
setfootattributes(dFoo,3);
}
(这需要您将
{a,b,c,d}接口支持点
{a,b,c,d}InterfaceRate
{a,b,c,d}接口主
{a,b,c,d}接口长度
转换为数组,而不是四个单独的变量)


<>你可以通过使用循环或变量的模板函数来改进这个,但是我将把它作为练习。< /P>问题是关于C++的基础。调用Foo::setfootattributes的方式仅适用于静态函数。否则,您需要完全按照错误所说的去做——拥有一个Foo类的对象。福福;f、 setfootattributes();嗨,泰,请尽快回复。我希望一次为多个对象设置属性,而不是a.setAttributes、b.setAttributes等。理想情况下,调用是“setFootAttributes(a、b、c、d);”,但编译器担心范围。我不知道如何解决范围问题,所以我按照上面所示制作了它。您可以将函数设置为非类函数。只需将它移出类并调用它,而不使用
Foo::
@buggfinger-ty作为建议。我不知道现在该怎么办。我一心想让这个函数成为Foo类的一部分,但它只有在该类之外才起作用。还有什么我可以让它在那个班里工作吗?@Duhmfuhk:好吧,你已经得到了让它静止的答案。。。是将其设置为静态还是将其移到类外取决于用例。两者都可以。更多关于非会员功能的信息:感谢您的回答!当我们讨论这个主题时,您可能不知道为什么mainwindow.h中的A、B、C、D对象声明会给出错误:“类不命名类型”?它发生在我第一次将相关函数更改为static时。此外,编译器现在抱怨错误“在“.”标记之前应该有主表达式”。setFooAttributesAh中的每个成员初始化都是这样说的
MainWindow
是一个类,对吗,不是
Foo
的成员?因此,在每种情况下,
MainWindow.ui
都应该是
MainWindow::ui
。这当然是假设
ui
成员是静态的;如果不是,则需要将其设置为静态,或者使用对试图通过类访问实例成员的
main窗口
实例的引用。关于“类不命名类型”,也许它不命名类型?实际上有命名为A、B、C和D的类型吗?您不显示它们的任何声明。