重新定义类函数错误C++
Redefinition of class function error C++
我有我的基类Gate,而派生的类是AND(XOR等)。 我在基类中的虚拟函数用于确定输出,但是当我在 AND.h 中对其进行原型并尝试使用 AND 实现它时.cpp我在编译时收到重新定义错误。 我相信我已经正确地包含了所有内容。
门头
#ifndef GATE_H
#define GATE_H
class Gate{
public:
// various functions etc.
virtual bool output(); // **PROBLEM FUNCTION**
};
#endif
门源
#include "Gate.h"
//*various function declarations but not virtual declaration*
派生类"AND"
#ifndef AND_H_INCLUDED
#define AND_H_INCLUDED
class AND: public Gate{
public:
bool output(bool A, bool B){}
};
#endif // AND_H_INCLUDED
以及我的 IDE 将我的错误放在 AND.h 文件中的位置
#include "AND.h"
bool AND::output(bool A, bool B){
if (A && B == true){
Out = true;
} else {
Out = false;
}
return Out;
}
在本例中为 Out 是一个继承变量。
您在此处定义AND
类定义中的方法AND::output
:
bool output(bool A, bool B){} // this is a definition
你在这里重新定义它:
bool AND::output(bool A, bool B){
if (A && B == true){
....
您可以通过将前者更改为声明来解决此问题:
bool output(bool A, bool B);
您提供了AND::output
的两个定义。一个在标头中,为空,另一个在实现文件中,不为空。看起来您的标头应该具有:
bool output(bool A, bool B);
请注意,您将无法多态地使用这些output
函数,因为它们与 Gate
中的声明没有相同的参数。
如前所述,您定义了两次函数输出:在标头和 cpp 模块中。此外,此函数不是虚拟函数,因为它的参数数量和类型与基类中具有相同名称的虚函数的声明不符。
我想补充一下,函数的定义可以简单得多
bool AND::output(bool A, bool B)
{
return ( Out = A && B );
}
相关文章:
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 由于签名差异,调用了错误的子类函数
- Tbb 库:错误:编写自定义类函数而不是 lambda 表达式时,对函数的调用不匹配
- 没有定义的类函数如何不会导致错误
- 当 C++ 中函数参数的输入类型(类)错误时的错误处理
- 尝试在构造函数中的"glm::cross"处初始化我的类段错误
- 如何修复类函数"prototype does not match"和"cadidate is"错误
- C++不使用"inline"或"static"无类函数的关键字时出现重定义链接错误
- C++执行期间的类成员函数错误/异常处理
- C++14 类函数错误:"Object has no attribute 'value'"
- 自定义类向量的类函数的分割错误
- 对于我的类函数,我得到双重释放或损坏错误
- 类组合中的构造函数错误
- 当创建从qt中的qgraphicspixmapitem创建类中时,尝试引用已删除的函数错误
- 简单矩阵类,错误:调用私有构造函数
- 如何从类调用函数 - 错误"was not in the scope"
- 定义模板类函数的错误
- 重新定义类函数错误C++
- 类函数错误C++
- 对类::函数错误的未定义引用