4

I'm writing a program to test concrete inheritance, though I cannot resolve the duplicate symbol linker error Clang returns. My understanding is duplicate symbols is always the result of incorrect includes/guards. I have triple checked my includes/guards, but I cannot find any errors. Could the duplicate symbol be the result of something other than include guards? Many thanks, as I grow my programming skills I intend to contribute here frequently.

.h

#ifndef POINTARRAY_H
#define POINTARRAY_H
#include "array.h"

namespace Jules
{
    namespace Containers
    {
        class PointArray: public Array<Point>
        {
        public:
            PointArray();    //default constructor
            ~PointArray();    //destructor
            PointArray(const PointArray& p);    //copy constructor
            PointArray(const int i);    //constructor with input argument
            PointArray& operator = (const PointArray& source);    //assignment operator
            double Length() const;    //length between points in array
        };
    }
}
#ifndef POINTARRAY_CPP
#include "PointArray.cpp"
#endif
#endif

.cpp

#ifndef POINTARRAY_CPP
#define POINTARRAY_CPP
#include "PointArray.h"

using namespace Jules::CAD;

namespace Jules
{
    namespace Containers
    {
        PointArray::PointArray() : Array<Point>()    //default constructor
        {
        }

        PointArray::~PointArray()    //destructor
        {
        }

        PointArray::PointArray(const PointArray& p) : Array<Point>(p)    //copy constructor
        {
        }

        PointArray::PointArray(const int i) : Array<Point>(i)    //constructor with input argument
        {
        }

        PointArray& PointArray::operator = (const PointArray& source)    //assignment operator
        {
            if (this == &source)
                return *this;
            PointArray::operator = (source);
            return *this;
        }

        double PointArray::Length() const
        {
        double lengthOfPoints = 0;
        for (int i = 0; i < Array::Size()-1; i++)
            lengthOfPoints += (*this)[i].Distance((*this)[i+1]);
            return lengthOfPoints;
        }
    }
}
#endif

Update: Thank you everyone for your help. I understand the mechanics now.

kits
  • 609
  • 6
  • 20
  • Include guards are not related to multiple definition link errors. (You might get multiple declaration/definition compilation errors before the link stage is reached, without them) – M.M Feb 18 '15 at 05:10

2 Answers2

2

Don't include the cpp file in your header. If you do this, every translation unit which includes your header will end up with a definition of the class, e.g., PointArray leading to the linker error with multiple definitions.

Remove this from your header.

#ifndef POINTARRAY_CPP
#include "PointArray.cpp"
#endif
#endif
Pradhan
  • 16,391
  • 3
  • 44
  • 59
1

You are #includeing your .cpp file in your .h, which will result in the .cpp code being included in every file that uses the .h (hence the duplicate symbols). You are also misusing include guards: only header files need include guards; .cpp files should never have them.

nneonneo
  • 171,345
  • 36
  • 312
  • 383