2016-06-18 83 views
-1

在我的mainwindow.cpp中是調用的地方。呼叫應該發生在點擊按鈕的事件上。錯誤說cannot call member function 'void Foo::setFooAttributes(A&, B&, C&, D&)' without objectQT,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

+2

問題是關於基本的C++。您調用Foo :: setFooAttributes的方式僅適用於靜態函數。否則,你需要做的是錯誤所說的 - 擁有Foo類的對象。 Foo f; f.setFooAttributes(); – mvidelgauz

+0

嗨,ty快速回復。我想一次爲多個對象設置屬性,而不是a.setAttributes,b.setAttributes等。理想情況下,調用將是「setFooAttributes(a,b,c,d);」但編譯器對範圍持懷疑態度。我不知道如何解決範圍問題,所以我如上所示 – Ranfan

+0

您可以使該函數成爲非類函數。只要將它移出課程並在沒有'Foo ::'的情況下調用它。 – Bugfinger

回答

3

讓你的Foo::setFooAttributes a static mem ber函數,因爲它不在「this」實例上運行。

當你在這,你可能會考慮刪除所有重複,以更好地遵循DRY(不要重複自己)原則:

template <typename A> static void Foo::setFooAttributes(A &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(A &aFoo, B &bFoo, C &cFoo, D &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)轉換爲數組,而不是四個單獨的變量)。

你甚至可以通過使用循環或變量模板函數來改善這一點,但我會把它作爲練習。

+0

謝謝你的特殊答案!當我們談論這個話題時,你不會知道爲什麼mainwindow.h中的A,B,C,D對象聲明給出了錯誤:「類沒有聲明類型」?它發生在我第一次將功能改變爲靜態時。此外,編譯器現在抱怨錯誤「之前的預期主表達式」。令牌「。它對於setFooAttributes – Ranfan

+0

中的每個成員初始化都這樣說。 'MainWindow'是一個類,正確的,不是'Foo'的成員?因此,在任何情況下,'MainWindow.ui'都應該是'MainWindow :: ui'。這當然假設'ui'成員是靜態的;如果不是,那麼你需要使它成爲靜態的,或者使用對試圖通過類訪問實例成員的'MainWindow'實例的引用。關於「班級不命名」,也許它不是?實際上是否有名爲A,B,C和D的類型?你不顯示任何他們的聲明。 – davmac

相關問題