且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

包含头文件错误:多个定义

更新时间:2022-04-11 22:52:38

为什么只有变量而不是函数有多个定义错误?据我所知,这两个都只在头文件worker中声明,而未定义.h

Why there are multiple definition errors for only variables and not for functions ? As far as my understanding goes both of those are only declared and not defined in the header file worker.h

因为您定义了变量.这样,仅声明它们:

Because you defined the variables. This way they are only declared :

extern bool x;
extern int y;

但是您必须在cpp文件中定义它们.:

But you have to define them in a cpp file. :

bool x = true;
int y = 42;