-1

Actually my question is simple. I am defining two separate classes and I overload + operator in the first class to accept the second class as its operand. But there is a linking problem (for now I am omitting const for member function). Please note that if I move the implementation of the + operator to the header file, the problem will be solved. But I want to have separate interface and implementation.

Here is the base class in file elementBase.h:

typedef double real;

#ifndef __elementBase_H__
#define __elementBase_H__

      class elementBase
        {
        public:

            // return the sphere diameter enclosing the whole element 
            virtual real boxDiameter() = 0;

            // return the longest size of the element 
            virtual real longestSize() = 0;

            // return the smallest size of the element
            virtual real smallestSize() = 0;

        protected:
        };
#endif

this is the first derived class in file sphereElement.h:

#ifndef __sphereElement_H__
#define __sphereElement_H__

#include "elementBase.h"
#include "sphereElement2.h"


class sphereElement : public elementBase
{
public:

    // constructors
    // null constructor
    sphereElement();

    // make the element with its diameter
    sphereElement(real d);

    // return the sphere radius enclosing the whole element 
    virtual real boxDiameter();

    // return the diameter of the sphere
    inline virtual real diameter();

    inline real operator + (sphereElement2 & oprnd2);


protected:
    real diam_;

};

the implementation in file sphereElement.cpp:

#include "sphereElement.h"

sphereElement::sphereElement() : diam_(0)
{
}

// make the element with its diameter
sphereElement::sphereElement(real d) : diam_(d)
{
}

// return the sphere radius enclosing the whole element 
real sphereElement::boxDiameter() 
{
    return diam_;
}

inline real sphereElement::diameter()
{
    return diam_;
}


inline real sphereElement::operator + (sphereElement2 & oprnd2)
{
    return diameter() + oprnd2.boxDiameter();

}

The second class I derived from base class in file sphereElement2.h:

#ifndef __sphereElement2_H__
#define __sphereElement2_H__

#include "elementBase.h"   


class sphereElement2 : public elementBase
{
public:

    // constructors
    // null constructor
    sphereElement2();

    // make the element with its diameter
    sphereElement2(real d);

    // return the sphere radius enclosing the whole element 
    virtual real boxDiameter();

protected:
    real diam_;

};

#endif

and its implementation in file sphereElement2.cpp:

#include "sphereElement2.h"
#include "constants.h"

sphereElement2::sphereElement2() : diam_(0)
{
}

// make the element with its diameter
sphereElement2::sphereElement2(real d) : diam_(d)
{
}

// return the sphere radius enclosing the whole element 
real sphereElement2::boxDiameter()
{
    return diam_;
}

when I want to use the + operator, I get the following error:

sphereElement SS1(4);
sphereElement2 SS2(5);

cout<< SS1 + SS2 << endl;   
error:
Error 1   error LNK2019: unresolved external symbol "public: double 
__thiscall sphereElement::operator+(class sphereElement2 &)" (??
HsphereElement@@QAENAAVsphereElement2@@@Z) referenced in function _wmain  
YSC
  • 38,212
  • 9
  • 96
  • 149
  • 2
    how are you building the program? – UKMonkey Mar 19 '18 at 16:49
  • Go easy on the `inline` here, your compiler will do that if and when it thinks it's important and you've set the optimization level high enough. – tadman Mar 19 '18 at 16:49
  • 1
    A question starting with _"Actually my question is simple"_ should not end with 200 lines of code. Please learn to build a [mcve]. – YSC Mar 19 '18 at 16:50
  • @YSC: you should've seen the question that was asked before hand! There were 1000+ lines :) This is much better (still not good; but I'll reward the effort) :) – UKMonkey Mar 19 '18 at 16:51
  • @UKMonkey I've seen it XD – YSC Mar 19 '18 at 16:52
  • 1
    Probably unrelated but could bite elsewhere: [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 Mar 19 '18 at 16:56
  • You are right. It should be shorter. Sorry. – user3672271 Mar 19 '18 at 17:11
  • 1
    And thanks, It worked. @tadman – user3672271 Mar 19 '18 at 17:12

1 Answers1

1

The linking error is caused by use of inline in the declaration and definition. Remove them.

real operator + (sphereElement2 & oprnd2);

and

real sphereElement::operator + (sphereElement2 & oprnd2)
{
    return diameter() + oprnd2.boxDiameter();
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270