-1

I'm writing a code describing a complex variable and a real variable.

i've included the header file which seems to produce a conflict between the .hpp file and the .cpp file.

They are similiar and I can't track down from where the redefinition happen.

solver.hpp


#include <iostream>
#include <complex>

class RealVariable
    {
        public:
        std::complex<double>c;
        RealVariable(double d);
        RealVariable();
   };

    class ComplexVariable
    {
        public:
        std::complex <double>d;
        ComplexVariable(double d1,double d2);
        ComplexVariable();

    };

solver.cpp

#include "solver.hpp"
#include <iostream>
#include <complex>

class RealVariable
    {
        public:
        std::complex<double>c;
        RealVariable(double d)
          {
            c.imag(0);
            c.real(d);
          }
        RealVariable(){}
   };

    class ComplexVariable
    {
        public:
        std::complex <double>d;
        ComplexVariable(double d1,double d2)
         {
          d.imag(d2);
          d.real(d1);
         }
        ComplexVariable(){}

    };

On the surface, the definition in .hpp and .cpp file looks identical(at least to me).

I get this error in the terminal: enter image description here

user6394019
  • 121
  • 5

2 Answers2

1

In your CPP file only the implementations should go.

#include "solver.hpp"

RealVariable::RealVariable(double d)
{
    c.imag(0);
    c.real(d);
}

RealVariable::RealVariable()
{
}

ComplexVariable::ComplexVariable(double d1,double d2)
{
  d.imag(d2);
  d.real(d1);
}

ComplexVariable::ComplexVariable()
{
}
RvdK
  • 19,580
  • 4
  • 64
  • 107
0

You can't have two definitions of the same class, even if they are 'identical'. Your .hpp file provides the class definitions, so you can't have those (again) in your .cpp file.

Instead, just provide the required definitions of the member functions in your .cpp file:

#include "solver.hpp"
#include <iostream>
#include <complex>

RealVariable::RealVariable(double d)
{
    c.imag(0);
    c.real(d);
}

RealVariable::RealVariable(){} // Though this could easily go 'inline' in the .hpp file

ComplexVariable::ComplexVariable(double d1,double d2)
{
    d.imag(d2);
    d.real(d1);
}

ComplexVariable::ComplexVariable(){} // Again, could readily be inline in the header
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83