錯誤:請求'..'中的成員'..',這是非類型的我有一個帶有兩個構(gòu)造函數(shù)的類,一個不帶參數(shù),另一個帶一個參數(shù)。使用帶有一個參數(shù)的構(gòu)造函數(shù)創(chuàng)建對象可以按預(yù)期工作。但是,如果我使用不帶參數(shù)的構(gòu)造函數(shù)創(chuàng)建對象,我會收到錯誤。例如,如果我編譯此代碼(使用g ++ 4.0.1)...class Foo{ public: Foo() {}; Foo(int a) {}; void bar() {};};int main(){ // this works... Foo foo1(1); foo1.bar(); // this does not... Foo foo2(); foo2.bar(); return 0;}...我收到以下錯誤:nonclass.cpp: In function ‘int main(int, const char**)’:nonclass.cpp:17: error: request for member ‘bar’ in ‘foo2’, which is of non-class type ‘Foo ()()’為什么這樣,我如何使它工作?
3 回答

蕪湖不蕪
TA貢獻1796條經(jīng)驗 獲得超7個贊
Foo foo2();
改成
Foo foo2;
你得到錯誤,因為編譯器認為
Foo foo2()
具有名稱'foo2'的函數(shù)聲明和返回類型'Foo'。
但在這種情況下,如果我們改為Foo foo2
,編譯器可能會顯示錯誤 " call of overloaded ‘Foo()’ is ambiguous"
。

翻過高山走不出你
TA貢獻1875條經(jīng)驗 獲得超3個贊
僅供記錄..
它實際上不是您的代碼的解決方案,但是當錯誤地訪問指向的類實例的方法時,我有相同的錯誤消息myPointerToClass
,例如
MyClass* myPointerToClass = new MyClass();myPointerToClass.aMethodOfThatClass();
哪里
myPointerToClass->aMethodOfThatClass();
顯然是正確的。

寶慕林4294392
TA貢獻2021條經(jīng)驗 獲得超8個贊
添加到知識庫,我得到了相同的錯誤
if(class_iter->num == *int_iter)
盡管IDE為我提供了class_iter的正確成員。顯然,問題是"anything"::iterator
沒有一個成員被叫,num
所以我需要取消引用它。這不是這樣的:
if(*class_iter->num == *int_iter)
...很明顯。我最終解決了這個問題:
if((*class_iter)->num == *int_iter)
我希望這可以幫助那些以我的方式遇到這個問題的人。
- 3 回答
- 0 關(guān)注
- 658 瀏覽
添加回答
舉報
0/150
提交
取消