C++ 错误:重新定义类,尽管使用了include-guard
我是一名javascript/typescript开发人员,但还是Arduino/c的新手++ 我有一个类(请参见下面的h和cpp),并且有以下编译器错误:C++ 错误:重新定义类,尽管使用了include-guard,c++,class,constructor,arduino,function-definition,C++,Class,Constructor,Arduino,Function Definition,我是一名javascript/typescript开发人员,但还是Arduino/c的新手++ 我有一个类(请参见下面的h和cpp),并且有以下编译器错误: DotMatrix.cpp:13:1: error: redefinition of 'DotMatrix::DotMatrix(uint8_t, MD_MAX72XX::moduleType_t, uint8_t, uint8_t, uint8_t)' DotMatrix::DotMatrix(uint8_t maxDevices, M
DotMatrix.cpp:13:1: error: redefinition of 'DotMatrix::DotMatrix(uint8_t, MD_MAX72XX::moduleType_t, uint8_t, uint8_t, uint8_t)'
DotMatrix::DotMatrix(uint8_t maxDevices, MD_MAX72XX::moduleType_t hardwareType, uint8_t clkPin, uint8_t dataPin, uint8_t csPin)
^
In file included from sketch/DotMatrix.cpp:5:0:
DotMatix.h:10:3: error: 'DotMatrix::DotMatrix(uint8_t, MD_MAX72XX::moduleType_t, uint8_t, uint8_t, uint8_t)' previously defined here
DotMatrix(uint8_t maxDevices, MD_MAX72XX::moduleType_t hardwareType, uint8_t clkPin, uint8_t dataPin, uint8_t csPin)
^
exit status 1
我不明白为什么
以下是文件:
DotMatix.h:
#ifndef DotMatrix_h
#define DotMatrix_h
#include <MD_MAX72xx.h>
#include <MD_Parola.h>
class DotMatrix
{
public:
DotMatrix(uint8_t maxDevices, MD_MAX72XX::moduleType_t hardwareType, uint8_t clkPin, uint8_t dataPin, uint8_t csPin)
: _parola(hardwareType, csPin, maxDevices)
{};
void setup();
private:
MD_Parola _parola;
};
#endif
我使用的是带有Arduino扩展的VS代码IDE。您应该更改DotMatrix的这一行。h:
DotMatrix(uint8_t maxDevices, MD_MAX72XX::moduleType_t hardwareType, uint8_t clkPin, uint8_t dataPin, uint8_t csPin)
: _parola(hardwareType, csPin, maxDevices)
{};
应该是:
DotMatrix(uint8_t maxDevices, MD_MAX72XX::moduleType_t hardwareType, uint8_t clkPin, uint8_t dataPin, uint8_t csPin)
: _parola(hardwareType, csPin, maxDevices);
如果不是,则直接在头文件中实现构造函数(构造函数什么也不做)。实现为“{}”(无需执行任何操作)
然后,当编译器在.cpp文件中看到另一个实现(正确的实现)时,它会说它已经被定义了
void func(); // function declaration
void func() { blablabla; } // function definition.
您可能会看到定义和声明之间的简单区别。一个有{
,另一个没有。一个(全局)函数在一个代码库中只能定义一次,而不是两次。否则编译器将不知道选择哪个函数-第一个?第二个
你做到了:
// in DotMatix.h:
DotMatrix(blablaa)
{}; // yes this is definition
而且还做了:
// in DotMatrix.cpp
DotMatrix::DotMatrix(ublabla, who cares)
{ // also definition
编译器会看到同一函数的两个定义,并带着错误退出
Include guard通常用于防止包含同一文件两次。您的多个定义包含在多个文件中。也不需要在源文件中包含guard-源文件并不意味着要包含,因此它们将始终被处理一次。您定义了两次构造函数。
Dot的第一个定义矩阵
位于标题DotMatix.h
中的类定义本身内:
class DotMatrix
{
public:
DotMatrix(uint8_t maxDevices, MD_MAX72XX::moduleType_t hardwareType, uint8_t clkPin, uint8_t dataPin, uint8_t csPin)
: _parola(hardwareType, csPin, maxDevices)
{};
//...
请注意,构造函数定义之后的null语句;
是多余的
此定义定义了一个内联函数
请注意,构造函数有未使用的参数
构造函数DotMatrix
的第二个定义在模块DotMatix.cpp
中:
DotMatrix::DotMatrix(uint8_t maxDevices, MD_MAX72XX::moduleType_t hardwareType, uint8_t clkPin, uint8_t dataPin, uint8_t csPin)
: _parola(hardwareType, csPin, maxDevices)
{
//MD_Parola(MD_MAX72XX::moduleType_t mod, uint8_t csPin, uint8_t numDevices = (uint8_t)'\001')
_parola = MD_Parola(hardwareType, csPin, maxDevices);
}
此外,此定义还包含构造函数主体内数据成员\u parola
的冗余赋值
在模块DotMatix.cpp
中完全删除此构造函数定义
还有这些警卫指令
#ifndef DotMatrix_cpp
#define DotMatrix_cpp
没有太大意义,因为模块不是为包含在其他模块中而设计的。感谢所有的输入!
我已经删除了cpp文件中的构造函数,现在一切正常。
另外,在cpp文件中包含守卫确实是一件愚蠢的事情
来自js/typescript的整个头文件和奇怪的初始化器列表语法仍在我脑海中创建快捷方式;-)
关于初始化器列表:这是使用参数构造函数声明类变量的唯一方法吗?我觉得这很奇怪…\ifndef DotMatrix\u cpp\define DotMatrix\u cpp
不,源文件没有包含保护。您定义了两个DotMatrix::DotMatrix
函数。错误甚至告诉我们你在哪里。
DotMatrix::DotMatrix(uint8_t maxDevices, MD_MAX72XX::moduleType_t hardwareType, uint8_t clkPin, uint8_t dataPin, uint8_t csPin)
: _parola(hardwareType, csPin, maxDevices)
{
//MD_Parola(MD_MAX72XX::moduleType_t mod, uint8_t csPin, uint8_t numDevices = (uint8_t)'\001')
_parola = MD_Parola(hardwareType, csPin, maxDevices);
}
#ifndef DotMatrix_cpp
#define DotMatrix_cpp