簡體   English   中英

QT,C ++無法在沒有對象的情況下調用成員函數

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM