(重定义错误)在 C++ 中从一个基类创建多个继承 类
(Redefinition error) Create multiple inherting classes from one baseclass in C++
伟大的程序员们,你们好,
我是初学者,在这里遇到了一些麻烦。
有我的基类(sensor.h):
class sensor
{
private:
int sensor_id;
string sensor_name;
string sensor_type;
float reading;
public:
sensor();
sensor(int, char*, char*);
~sensor();
/* Few extra methods here */
};
...我想创建另外 4 个 类 继承自我的基类 sensor
(温度传感器、湿度传感器...等等)。
#include "sensor.h"
class temperaturesensor:public sensor
{
public:
Temperatursensor(int, char*,char*);
~Temperatursensor();
/* Few extra methods here */
};
事情是:这些 类 中的每一个都必须在其自己的 .cpp/.h 文件中,然后被包含并用于我的 main.cpp.
using namespace std;
#include <xyz.h>
/* Other libaries here */
....
#include "temperaturesensor.h"
#include "humiditysensor.h"
int main()
{
sensor* station[2];
station [0] = new temperaturesensor(x,y,z);
station [1] = new humiditysensor(x,y,z);
}
如果我包括其中之一,那没什么大不了的。但是:如果我使用多个,则会出现重新定义错误。
错误 C2011:'sensor':'class' 类型重新定义
c:\users\name\desktop\project\sensor.h 14
error c2011: 'temperaturesensor' : 'class' typeredefinition
我该怎么做才能解决这个问题?请注意,我不允许使用 #pragma once
抱歉我的愚蠢,在此先感谢!
您忘记在 header class 中使用 include guards,
这会在您每次使用时重新定义您的基础 class。
所以,做一个
#pragma once
或普通的包含保护
#ifndef YOURFILENAME_H
#define YOURFILENAME_H
.. normal code here
#endif
这样就不会出现多重定义错误了
class sensor
的定义来自 "temperaturesensor.h"
和 "humiditysensor.h"。使用守卫 https://en.wikipedia.org/wiki/Include_guard or #pragma once
: https://en.wikipedia.org/wiki/Pragma_once
您必须使用:
#ifndef FILE_H
#define FILE_H
.. normal code here
#endif
或
#pragma once
但我认为,传感器也应该是抽象的 class,并且您应该使用虚拟析构函数。
另一种想法是数组是从 0 开始计算的。
伟大的程序员们,你们好,
我是初学者,在这里遇到了一些麻烦。
有我的基类(sensor.h):
class sensor
{
private:
int sensor_id;
string sensor_name;
string sensor_type;
float reading;
public:
sensor();
sensor(int, char*, char*);
~sensor();
/* Few extra methods here */
};
...我想创建另外 4 个 类 继承自我的基类 sensor (温度传感器、湿度传感器...等等)。
#include "sensor.h"
class temperaturesensor:public sensor
{
public:
Temperatursensor(int, char*,char*);
~Temperatursensor();
/* Few extra methods here */
};
事情是:这些 类 中的每一个都必须在其自己的 .cpp/.h 文件中,然后被包含并用于我的 main.cpp.
using namespace std;
#include <xyz.h>
/* Other libaries here */
....
#include "temperaturesensor.h"
#include "humiditysensor.h"
int main()
{
sensor* station[2];
station [0] = new temperaturesensor(x,y,z);
station [1] = new humiditysensor(x,y,z);
}
如果我包括其中之一,那没什么大不了的。但是:如果我使用多个,则会出现重新定义错误。
错误 C2011:'sensor':'class' 类型重新定义 c:\users\name\desktop\project\sensor.h 14
error c2011: 'temperaturesensor' : 'class' typeredefinition
我该怎么做才能解决这个问题?请注意,我不允许使用 #pragma once
抱歉我的愚蠢,在此先感谢!
您忘记在 header class 中使用 include guards, 这会在您每次使用时重新定义您的基础 class。
所以,做一个
#pragma once
或普通的包含保护
#ifndef YOURFILENAME_H
#define YOURFILENAME_H
.. normal code here
#endif
这样就不会出现多重定义错误了
class sensor
的定义来自 "temperaturesensor.h"
和 "humiditysensor.h"。使用守卫 https://en.wikipedia.org/wiki/Include_guard or #pragma once
: https://en.wikipedia.org/wiki/Pragma_once
您必须使用:
#ifndef FILE_H
#define FILE_H
.. normal code here
#endif
或
#pragma once
但我认为,传感器也应该是抽象的 class,并且您应该使用虚拟析构函数。 另一种想法是数组是从 0 开始计算的。