从模板派生时LNK2019错误

时间:2013-08-05 14:06:06

标签: c++

我正在尝试实现从模板派生的PointArray类。这是我的PointArray的hpp文件:

   #ifndef POINTARRAY_H
#define POINTARRAY_H
#include <iostream>
#include <sstream>
#include <stdio.h>
#include "Array.hpp"

using namespace Abhishek::CAD;
using namespace Abhishek::CONTAINERS;

namespace Abhishek
{
    namespace CONTAINERS
    {
        class PointArray : public Array<Point>
        {
        public:
            PointArray();//Default constrcutor.
            PointArray(int size);//Constructor with size argument.
            PointArray(const PointArray& arr);//Copy constructor.
            ~PointArray();//Destructor.
            double Length() const;//Length function.
        };

    }
}


#endif

我的cpp看起来像这样:

    #include <iostream>
#include <sstream>
#include <stdio.h>
#include "PointArray.hpp"

using namespace Abhishek::CAD;
using namespace Abhishek::CONTAINERS;

namespace Abhishek
{
    namespace CONTAINERS
    {
        //Default constructor.
        PointArray::PointArray(): Array<Point>()
        {
            cout<<"Point arr default cons"<<endl;
        }

        //Constructor with size argument.
        PointArray::PointArray(int size) : Array<Point>(size)
        {

        }

        //Copy constructor.
        PointArray::PointArray(const PointArray& arr) : Array<Point>(arr)
        {

        }

        //destrcutor.
        PointArray::~PointArray()
        {

        }
    }
}

我收到了LNK错误:

     error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::Array<class Abhishek::CAD::Point>(void)" (??0?$Array@VPoint@CAD@Abhishek@@@CONTAINERS@Abhishek@@QAE@XZ) referenced in function "public: __thiscall Abhishek::CONTAINERS::PointArray::PointArray(void)" (??0PointArray@CONTAINERS@Abhishek@@QAE@XZ)
1>PointArray.obj : error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::Array<class Abhishek::CAD::Point>(int)" (??0?$Array@VPoint@CAD@Abhishek@@@CONTAINERS@Abhishek@@QAE@H@Z) referenced in function "public: __thiscall Abhishek::CONTAINERS::PointArray::PointArray(int)" (??0PointArray@CONTAINERS@Abhishek@@QAE@H@Z)
1>PointArray.obj : error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::Array<class Abhishek::CAD::Point>(class Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point> const &)" (??0?$Array@VPoint@CAD@Abhishek@@@CONTAINERS@Abhishek@@QAE@ABV012@@Z) referenced in function "public: __thiscall Abhishek::CONTAINERS::PointArray::PointArray(class Abhishek::CONTAINERS::PointArray const &)" (??0PointArray@CONTAINERS@Abhishek@@QAE@ABV012@@Z)
1>PointArray.obj : error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::~Array<class Abhishek::CAD::Point>(void)" (??1?$Array@VPoint@CAD@Abhishek@@@CONTAINERS@Abhishek@@QAE@XZ) referenced in function __unwindfunclet$??0PointArray@CONTAINERS@Abhishek@@QAE@XZ$0
1>C:\Users\Rambo\Documents\Level 6\Section 4.2b\Exercise 3\Debug\Exercise 3.exe : fatal error LNK1120: 4 unresolved externals

我不明白为什么会发生这种情况。我包括了所有相关的头文件和CPP文件。如果有人可以提供帮助,我将非常感激。

1 个答案:

答案 0 :(得分:0)

您忘记发布最相关的标头:声明导致错误的类模板的标头。几乎可以肯定,该标头声明了Array模板的默认构造函数:

Array();

但没有定义;要么没有定义,要么在源文件中有定义。

在任何一种情况下,您都会收到错误,因为必须在使用模板的任何翻译单元中定义模板。这意味着您需要在标题中定义构造函数(以及任何其他成员函数),以便在任何使用它们的地方包含它们。

如果这不是问题,请发布标题,以便我们进一步调查。