2013-04-26 58 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; 
};