0

I am trying to implement a PointArray class derived from a template. Here is what my hpp file for PointArray looks like:

   #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

My cpp looks like this :

    #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()
        {

        }
    }
}

I get the LNK error :

     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

I dont understand why this could be happening. I included all the relevant header files and CPP files. If anyone can help I will really appreciate it.

Rambo223
  • 5
  • 3
  • Where is the class `Point` defined? – juanchopanza Aug 05 '13 at 14:08
  • 3
    Let me guess: the implementation of the `Array` template is in "Array.cpp", not in "Array.hpp"? – molbdnilo Aug 05 '13 at 14:12
  • 2
    [What is an unresolved external and how do I fix it? Answer: Template implementations not visible](http://stackoverflow.com/a/12574417/673730). – Raymond Chen Aug 05 '13 at 15:13
  • Hi I actually just figured it out. I wasnt including the Array.cpp file in my PointArray.hpp once I did that the errors were gone and my constructors were getting called properly. Thanks for looking at it. – Rambo223 Aug 05 '13 at 15:20

1 Answers1

0

You forgot to post the most relevant header: the one that declares the class template that causes the error. Almost certainly, that header declares the default constructor of the Array template:

Array();

but doesn't define it; either there is no definition, or you have a definition in a source file.

In either case, you'll get an error since templates must be defined in any translation unit that uses them. This means that you'll need to define the constructor (and any other member functions) in the header, to include them wherever they are used.

If that's not the problem, then please post the header so we can investigate further.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • I actually just figured it out. I wasnt including the Array.cpp file in my PointArray.hpp once I did that the errors were gone and my constructors were getting called properly. Thanks for looking at it. – Rambo223 Aug 05 '13 at 15:35
  • @user2653401: You might want to rename `Array.cpp` (or merge it with `Array.hpp`) since it's a header, not a source file. Using misleading file extensions can confuse both the compiler and the programmer. – Mike Seymour Aug 05 '13 at 15:41