monitoring_point.h头文件内容如下: #ifndef MONITORING_POINT_H #define MONITORING_POINT_H #endif // MONITORING_POINT_H #include<QString> #include"monitoring_sensor.h" using namespace std; class Monitoring_point { private: QString JCD_Name;//监测点名称 QString JCD_Type;//监测点所检测的数据类型 QString JCD_Number;//监测点编号 public: Monitoring_sensor sensor;//监测点有传感器 void set_JCD_Name(QString n); void set_JCD_Type(QString t); void set_JCD_Number(QString n); QString get_JCD_Name(); QString get_JCD_Type(); QString get_JCD_Number(); }; monitoring_point.cpp源文件内容如下: #include"monitoring_point.h" #include"mainwindow.h" using namespace std; void Monitoring_point::set_JCD_Name(QString n) { JCD_Name = n; } void Monitoring_point::set_JCD_Type(QString t) { JCD_Type = t; } void Monitoring_point::set_JCD_Number(QString n) { JCD_Number = n; } QString Monitoring_point::get_JCD_Name() { return JCD_Name; } QString Monitoring_point::get_JCD_Type() { return JCD_Type; } QString Monitoring_point::get_JCD_Number() { return JCD_Number; } monitoring_sensor.h头文件内容如下: #ifndef MONITORING_SENSOR_H #define MONITORING_SENSOR_H #endif // MONITORING_SENSOR_H #include<QString> using namespace std; class Monitoring_sensor { private: QString CGQ_Number;//传感器编号 QString CGQ_State;//传感器状态 QString CGQ_Manufacturer;//生产商 QString CGQ_Outnumber;//出厂号 public: void set_CGQ_Number(QString n); QString get_CGQ_Number(); void set_CGQ_State(QString s); QString get_CGQ_State(); void set_CGQ_Manufacturer(QString m); QString get_CGQ_Manufacturer(); void set_CGQ_Outnumber(QString o); QString get_CGQ_Outnumber(); }; monitoring_sensor.cpp源文件内容如下: #include "monitoring_sensor.h" #include"mainwindow.h" using namespace std; void Monitoring_sensor::set_CGQ_Number(QString n) { CGQ_Number=n; } QString Monitoring_sensor::get_CGQ_Number() { return CGQ_Number; } void Monitoring_sensor::set_CGQ_State(QString s) { CGQ_State = s; } QString Monitoring_sensor::get_CGQ_State() { return CGQ_State; } void Monitoring_sensor::set_CGQ_Manufacturer(QString m) { CGQ_Manufacturer = m; } QString Monitoring_sensor::get_CGQ_Manufacturer() { return CGQ_Manufacturer; } void Monitoring_sensor::set_CGQ_Outnumber(QString o) { CGQ_Outnumber=o; } QString Monitoring_sensor::get_CGQ_Outnumber() { return CGQ_Outnumber; } 为什么报错E:\qiaoliangjiance\monitoring_sensor.h:8: error: redefinition of 'class Monitoring_sensor' class Monitoring_sensor ^~~~~~~~~~~~~~~~~
时间: 2024-04-09 18:30:39 浏览: 65
监控系统编号说明.doc
报错的原因是在头文件"monitoring_sensor.h"中重新定义了类"Monitoring_sensor"。这通常是由于多次包含同一头文件引起的。在你的代码中,"monitoring_point.h"和"monitoring_sensor.h"头文件互相包含了对方的头文件,导致了重定义错误。
为了解决这个问题,你可以使用前置声明来代替头文件的包含。在"monitoring_point.h"和"monitoring_sensor.h"头文件中,将对方的头文件包含移动到源文件中,并使用类的前置声明来声明类的成员。以下是修改后的代码示例:
monitoring_point.h:
#ifndef MONITORING_POINT_H
#define MONITORING_POINT_H
#include <QString>
class Monitoring_sensor; // 前置声明
class Monitoring_point
{
private:
QString JCD_Name;
QString JCD_Type;
QString JCD_Number;
public:
Monitoring_sensor* sensor; // 使用指针
void set_JCD_Name(QString n);
void set_JCD_Type(QString t);
void set_JCD_Number(QString n);
QString get_JCD_Name();
QString get_JCD_Type();
QString get_JCD_Number();
};
#endif // MONITORING_POINT_H
monitoring_sensor.h:
#ifndef MONITORING_SENSOR_H
#define MONITORING_SENSOR_H
#include <QString>
class Monitoring_sensor
{
private:
QString CGQ_Number;
QString CGQ_State;
QString CGQ_Manufacturer;
QString CGQ_Outnumber;
public:
void set_CGQ_Number(QString n);
QString get_CGQ_Number();
void set_CGQ_State(QString s);
QString get_CGQ_State();
void set_CGQ_Manufacturer(QString m);
QString get_CGQ_Manufacturer();
void set_CGQ_Outnumber(QString o);
QString get_CGQ_Outnumber();
};
#endif // MONITORING_SENSOR_H
monitoring_point.cpp:
#include "monitoring_point.h"
#include "monitoring_sensor.h"
void Monitoring_point::set_JCD_Name(QString n)
{
JCD_Name = n;
}
void Monitoring_point::set_JCD_Type(QString t)
{
JCD_Type = t;
}
void Monitoring_point::set_JCD_Number(QString n)
{
JCD_Number = n;
}
QString Monitoring_point::get_JCD_Name()
{
return JCD_Name;
}
QString Monitoring_point::get_JCD_Type()
{
return JCD_Type;
}
QString Monitoring_point::get_JCD_Number()
{
return JCD_Number;
}
monitoring_sensor.cpp:
#include "monitoring_sensor.h"
void Monitoring_sensor::set_CGQ_Number(QString n)
{
CGQ_Number = n;
}
QString Monitoring_sensor::get_CGQ_Number()
{
return CGQ_Number;
}
void Monitoring_sensor::set_CGQ_State(QString s)
{
CGQ_State = s;
}
QString Monitoring_sensor::get_CGQ_State()
{
return CGQ_State;
}
void Monitoring_sensor::set_CGQ_Manufacturer(QString m)
{
CGQ_Manufacturer = m;
}
QString Monitoring_sensor::get_CGQ_Manufacturer()
{
return CGQ_Manufacturer;
}
void Monitoring_sensor::set_CGQ_Outnumber(QString o)
{
CGQ_Outnumber = o;
}
QString Monitoring_sensor::get_CGQ_Outnumber()
{
return CGQ_Outnumber;
}
阅读全文