C ++:重新定义“类名”

问题描述 投票:0回答:1

我正在编写描述复杂变量和实变量的代码。

我包含了头文件,似乎在.hpp文件和.cpp文件之间产生冲突。

它们很相似,我无法确定重新定义的位置。

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

    };

在表面上,定义.hpp和.cpp文件中的文件看起来相同(至少对我而言)。

我在终端中收到此错误:enter image description here

c++ header-files ambiguity redefinition
1个回答
0
投票

在您的CPP文件中,仅应执行。

#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()
{
}
© www.soinside.com 2019 - 2024. All rights reserved.