2013-04-26 56 views
3

我有以下基類和派生類結構。未定義的引用錯誤基類的所有方法

鹼/包含/ Base.h

namespace A 
{ 
namespace B 
{ 

class Base 
{ 
public: 
    Base(); 
    Base(const Type type, const Name name); 
    virtual ~Base(); 

    // Copy constructor 
    Base(const Base& other); 

    // Assignment operator 
    Base& operator= (const Base& rhs); 

    // Comparison 
    bool operator< (const Base& rhs); 

    std::string toString(); 

    //These enums have been defined in same file above the class 

    enum Type mType; 
    enum Name mName; 
}; 

} 
} 

鹼/ SRC/Base.cpp

#include "Base.h" 
//and other required includes 

namespace A 
{ 
namespace B 
{ 

Base::Base() 
{ 

} 

Base::Base(const Type type, const Name name) 
{ 

} 

Base::~Base() 
{ 

} 

// Copy constructor 
Base::Base(const Base& other) 
{ 

} 

// Assignment operator 
Base& Base::operator= (const Base& rhs) 
{ 

} 

// Comparison 
bool Base::operator< (const Base& rhs) 
{ 

} 

std::string Base::toString() 
{ 

} 
} 
} 

鹼/測試/ test.h

#include "Base.h" 
    namespace A 
    { 
    namespace B 
    { 
    class Derived: public Base 
    { 

     public: 
    Derived(); 
    Derived(const Type type, const Name name); 
    virtual ~Derived(); 

    Derived(const Derived& other); 
    Derived& operator= (const Derived& rhs); 

    virtual std::string toString(); 
}; 

    } 
    } 

鹼/測試/ test.cpp

#include "test.h" 
    namespace A 
    { 
    namespace B 
    { 


    Derived::Derived() 
    { 

    } 

    Derived::Derived(const Type type, const Name name) 
    :Base(type, name) 
    { 

    } 

    Derived::~Derived() 
    { 

    } 

    Derived::Derived(const Derived& other) 
    { 

    } 

    Derived& Derived::operator= (const Derived& rhs) 
    { 

    } 

    std::string Derived::toString() 
    { 

    } 
}; 

    } 
} 

現在,我正在爲基類目錄構建libBase.a。 然後我試圖編譯命令行派生類像這樣的基地/ test目錄:

/tmp/ccjLwXZp.o:Derived.cpp:(.text+0x10d): undefined reference to `A::B::Base::Base() 
/tmp/ccjLwXZp.o:Derived.cpp:(.text+0x129): undefined reference to `A::B::Base::Base() 
/tmp/ccjLwXZp.o:Derived.cpp:(.text+0x161): undefined reference to `A::B::base::Base(const Type type, const Name name)' 

這些錯誤,我得到了定義的所有功能:

g++ *.cpp -o Test \ 
    -I /cygdrive/c/projects/base/include \ 
    -L /cygdrive/c/projects/base/Build -lBase 

但我得到這樣的錯誤在基類中。 我不確定,我做錯了什麼。 我已經檢查了libBase.a是在適當的位置

+1

你如何構建'Base'共享對象? – juanchopanza 2013-04-26 09:27:21

+0

它是一個Visual Studio項目,它有幾個文件並生成.a – 2013-04-26 09:37:25

+1

好吧,看起來你沒有鏈接到'.a'。 – juanchopanza 2013-04-26 09:38:22

回答

0

的問題可能涉及到C-TOR聲明之後TypeName枚舉你聲明的事實。由於這個原因,編譯器無法構建Base類。

class Base 
{ 
public: 
    enum Type mType; // <-- define the type and Name here, before 
    enum Name mName; // <-- the C-tor (actual usage) 


    Base(); 
    Base(const Type type, const Name name); 
    virtual ~Base(); 

    // Copy constructor 
    Base(const Base& other); 

    // Assignment operator 
    Base& operator= (const Base& rhs); 

    // Comparison 
    bool operator< (const Base& rhs); 

    std::string toString(); 

    //These enums have been defined in same file above the class 

    //enum Type mType; // <<- your original location 
    //enum Name mName; 
};