2016-06-15 37 views
0

我正在爲一個學校任務使用C++僞圖形按鈕。 我使用觀察者模式。錯誤C2660:'MouseListener :: MousePressed':函數不需要4個參數

在Button.cpp

void Button::notify() 
{ 
    for (int iterator = 0; iterator < listeners.size(); iterator++) 
    { 
     listeners[iterator]->MousePressed(*this, x, y, isLeft); 
    } 
} 

,你可以看到功能的mousePressed接收4個參數,但我得到:

函數不接受4個參數

在按鈕.h:

struct MouseListener 
{ 
    virtual void MousePressed(Button &b, int x, int y, bool isLeft) = 0; 
}; 

class Button : public Label 
{ 
public: 

    vector <MouseListener*> listeners; 
. 
. 
. 

在主:

struct MyListener : public MouseListener 
{ 
    MyListener(iControl &c) : _c(c) { } 
    void MousePressed(Button &b, int x, int y, bool isLeft) 
    { 
     _c.setForeground(Color::Red); 
    } 
private: 
    iControl &_c; 
}; 

int main(VOID) 
{ 

錯誤:

  • 錯誤2錯誤C2061:語法錯誤:標識符 '按鈕' C:\用戶\戈南\ unitedproj \ unitedproj \ Button.h 14 1 unitedProj

  • 錯誤4錯誤C2061:語法錯誤:標識符 '按鈕' C:\用戶\戈南\ unitedproj \ unitedproj \ Button.h 14 1 unitedProj

  • 錯誤5錯誤C2660: '的MouseListener ::的mousePressed':函數不接受4個參數C:\用戶\戈南\ unitedProj \ unitedProj \ Button.cpp 24 1 unitedProj

當第一兩個用於main中的聲明,最後一個用於使用Button.cpp中的函數。 有幫助嗎?當IM用鼠標光標站在在Button.cpp功能我得到:

無效的MouseListener的mousePressed ::(按鈕& B,INT X,INT Y,布爾isLeft)

我只是需要這個工作,所以我可以繼續前進,讓其他組成員使用它們實現的控件中的按鈕...並繼續下一步我需要做的:-(

編輯:謝謝對於快速的回答。 我試圖做你所問的。現在我得到:

*錯誤3錯誤LNK2019:解析外部符號主要在功能__tmainCRTStartupÇ引用:\用戶\戈南\ unitedProj \ unitedProj \ MSVCRTD.LIB(crtexe.obj)未itedProj

+1

一個基本的語法錯誤可能會產生一系列其他錯誤。始終從錯誤列表的頂部開始,**從不**到底部。 –

+0

注意:'VOID'被定義爲'void',所以'int main(VOID)'看起來很奇怪,但會起作用。[Windows數據類型(Windows)](https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v = vs.85).aspx) – MikeCAT

回答

0

Button不宣佈MousePressed宣佈的地點,所以它不能使用。我建議你應該使用前向聲明。

class Button; // add this line 

struct MouseListener 
{ 
    virtual void MousePressed(Button &b, int x, int y, bool isLeft) = 0; 
}; 

class Button : public Label 
{ 
public: 

    vector <MouseListener*> listeners; 
. 
. 
. 
相關問題