-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  
8
  • 2
    how are you building the program? Commented Mar 19, 2018 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. Commented Mar 19, 2018 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 minimal reproducible example. Commented Mar 19, 2018 at 16:50
  • 1
    Probably unrelated but could bite elsewhere: What are the rules about using an underscore in a C++ identifier? Commented Mar 19, 2018 at 16:56
  • 1
    And thanks, It worked. @tadman Commented Mar 19, 2018 at 17:12

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

@user3672271, you're welcome.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.