0

我正在尝试实现从模板派生的 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 文件。如果有人可以提供帮助,我将不胜感激。

4

1 回答 1

0

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

Array();

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

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

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

于 2013-08-05T14:25:43.713 回答