首页 > 解决方案 > C++ 中的多个类定义错误,我的头文件有问题吗?

问题描述

我只在 .hpp 文件中声明该类一次,并在另一个 .cpp 文件中实现其成员函数。按该顺序排列的 .cpp 和 .hpp 文件如下所示。我在 .cpp 文件中得到错误。第一个是在第 6 行,说“complex::complex 的多重定义”和“这里首先定义”这发生在每个函数上,包括重载的 ctor。当我尝试将它们的原型包含在 .hpp 文件中时,我是否无意中定义了所有这些函数?

只要我在使用该类的主文件中包含 .hpp 文件而不是 .cpp ,我就不会收到任何错误。这有什么意义?编译器如何访问包含所有函数定义的 .cpp 文件?

#include <iomanip>      // For fixed, setprecision()
#include <sstream>      // For stringstream class
#include "Complex.hpp"  // For the complex class declaration


    complex::complex()
    {
        init(0,0);
    }
 
 complex::complex(double init_real, double init_imag)
 {
    init(init_real, init_imag);
 }


 double complex::get_imag()
 {
     return m_imag;
 }


    double complex::get_real()
    {
        return m_real;
    }


//--------------------------------------------------------------------------------------------------
void complex::init(double init_real, double init_imag)
    {
        m_real = init_real;
        m_imag = init_imag;
    }

void complex::set_imag(double s)
    {
        m_imag = s;
    }
-
void complex::set_real(double s)
    {
        m_real = s;
    }


std::string complex::to_string()
{
    std::stringstream sout;
    sout << std::fixed << std::setprecision(4);
    sout << "(" << get_real();
    double imag = get_imag();
    if (imag < 0) {
        sout << " - " << -imag << 'i';
    } else if (imag > 0) {
        sout << " + " << imag << 'i';
    }
    sout << ")";
    return sout.str();
}

#ifndef COMPLEX_HPP  // See the comments in the header comment block regarding these two lines.
#define COMPLEX_HPP

#include <string> // Included because we are using the string class in to_string()
class complex
{
    public:

    complex();

    complex(double , double);

    double get_imag();

    double get_real();

    void set_imag(double);

    void set_real(double);

    std::string to_string();

    private:

    void init(double , double );

    double m_real;

    double m_imag;
};

#endif

使用这些的主文件:

#include <fstream> // For ofstream class declaration
#include <iostream> // For cout, endl
#include "Complex.hpp" // So we can access the complex class declaration (hint: see Complex.cpp)
#include <string>


int main()
{
    std::string filename; filename = "complex-test.txt";
    std::ofstream...
    .
    .
    .
    return 0;
}

标签: c++

解决方案


包含一个 cpp 文件相当于在该文件中导入定义。这意味着您在实际定义的 cpp 文件中有一个定义,并且该定义静默地进入您包含的文件中。这样,链接器会为同一函数生成 2 个定义副本并感到困惑。编译应该没问题,但链接会导致问题。尝试避免 #include 中的 cpp 文件


推荐阅读