c++ - 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;
}
解决方案
包含一个 cpp 文件相当于在该文件中导入定义。这意味着您在实际定义的 cpp 文件中有一个定义,并且该定义静默地进入您包含的文件中。这样,链接器会为同一函数生成 2 个定义副本并感到困惑。编译应该没问题,但链接会导致问题。尝试避免 #include 中的 cpp 文件
推荐阅读
- java - 无法单击 goibibo 站点中的单选按钮,因为每个单选按钮可能具有相同的元素
- c - c - 如何正确释放哈希表上的元素
- ubuntu - 与服务器 10.117.xxx.xxx:6443 的连接被拒绝 - 您是否指定了正确的主机或端口?
- angular - Angular 7 主动路由链接失去了他的风格?
- vba - 错误 9:使用 open private 时下标超出范围
- vue.js - 如何将使用参数计算的参数传递给子组件的道具?
- c++ - Adding methods to class template specialization
- html - proc 报告中的 sas 图形符号未正确显示
- html - Bootstrap 4 将列表组与一些文本和下拉列表垂直对齐
- android - 如何使包含 GridView 的整个页面可滚动?