2014-02-13 54 views
0

我想在C++中編寫一個基本的gui庫,並且我遇到了表面上基本的繼承問題。我有一個基類組件在Component.h在子類之外實現導致未知錯誤的函數

class Component 
{ 
public: 
    virtual void add(Component &c); 
    virtual void remove(Component &c); 
    virtual void setBounds(int x, int y, int width, int height); 
    virtual void setLocation(int x, int y); 
    virtual void setSize(int width, int height); 
    virtual void setVisible(bool b); 
}; 

這裏delcared我這兒也有顯示在相同的標題宣告一個子幀

class Frame : public Component 
{ 
private: 
    char* ftitle; 
    HWND* hwnd; 

public: 
    Frame(); 
    Frame(char* title); 
    void add(Component &c); 
    void remove(Component &c); 
    void setBounds(int x, int y, int width, int height); 
    void setLocation(int x, int y); 
    void setSize(int width, int height); 
    void setVisible(bool b); 
    void setTitle(char* title); 
}; 

我實現這個功能的類在另一個文件名爲框架這裏顯示

#include "Component.h" 

Frame::Frame() 
{ 
    Frame(""); 
} 

Frame::Frame(char* title) 
{ 
    ftitle = title; 
    *hwnd = CreateWindow("static", title, WS_OVERLAPPEDWINDOW, 0, 0, 100, 100, NULL, NULL, GetModuleHandle(NULL), NULL); 
} 

void Frame::setVisible(bool visible) 
{ 
    if(visible) 
    { 
     ShowWindow(*hwnd, SW_SHOW); 
    } 
    else 
    { 
     ShowWindow(*hwnd, SW_HIDE); 
    } 
} 

void Frame::add(Component &c){} 
void Frame::remove(Component &c){} 
void Frame::setBounds(int x, int y, int width, int height){} 
void Frame::setLocation(int x, int y){} 
void Frame::setSize(int width, int height){} 
void Frame::setTitle(char* title){} 

的.cpp然而,當我嘗試編譯並生成項目,我得到這樣的

所示的幾個錯誤
1>------ Build started: Project: GUI, Configuration: Debug Win32 ------ 
1> Frame.cpp 
1> Generating Code... 
1> Compiling... 
1> Main.cpp 
1> Generating Code... 
1>Frame.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Component::add(class Component &)" ([email protected]@@[email protected]@Z) 
1>Frame.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Component::remove(class Component &)" ([email protected]@@[email protected]@Z) 
1>Frame.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Component::setBounds(int,int,int,int)" ([email protected]@@[email protected]) 
1>Frame.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Component::setLocation(int,int)" ([email protected]@@[email protected]) 
1>Frame.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Component::setSize(int,int)" ([email protected]@@[email protected]) 
1>Frame.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Component::setVisible(bool)" ([email protected]@@[email protected]) 
1>C:\Users\Owner\Documents\Visual Studio 2012\Projects\GUI\Debug\GUI.exe : fatal error LNK1120: 6 unresolved externals 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

回答

3

鏈接器抱怨你的Component類方法缺少實現,因爲它們不是純虛擬的。您可以通過使其成爲純虛擬來解決此問題:

virtual void add(Component &c) = 0; 

等等。

或者,可以提供實現。

請注意,你也應該給Component一個虛擬析構函數。

相關問題