4

次の基本クラスと派生クラス構造があります。

ベース/インクルード/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()
    {

    }
};

    }
 }

現在、Base クラス ディレクトリの libBase.a をビルドしています。そして、ベース/テストディレクトリで次のようにコマンドラインで派生クラスをコンパイルしようとしています:

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

しかし、次のようなエラーが発生します。

/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)'

これらのエラーは、基本クラスで定義されたすべての関数に対して発生します。私が間違っているのは何ですか。libBase.a が適切な場所にあることを確認しました

4

1 に答える 1

0

この問題は、c-tor 宣言の後に宣言Typeと列挙を行ったという事実に関連している可能性があります。Nameこのため、コンパイラは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;
};
于 2014-07-29T10:26:31.403 に答える