../Manager.o:(.data+0x0): multiple definition of Cdebug::n'
Cdebug::n'
/tmp/ccm2hcIk.o:(.data+0x0): first defined here
../Epoller.o:(.data+0x0): multiple definition of
/tmp/ccm2hcIk.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
Cdebug的源码如下:
#ifndef DEBUG_H
#define DEBUG_H
class Cdebug{
.....
private:
static int n;
}
int Cdebug::n=1;
#endif
这里我已经用#ifndef 防止头文件重复导入,为什么还会出错?
首先理解什么是.h文件,问什么要用.h文件,弄清楚这个就好了。 头文件要被别的文件包含,多个.cp文件包含这个头文件的时候,实际上会在编译的时候在不同的模块定义这个变量,所以会出现重复定义的问题,所以头文件只放变量的声明,.cpp里面放定义,#ifndef DEBUG_H只能防止同一个.cpp多次包含同一个头文件,不能避免多个.cpp包含同样的头文件。
是因为这里int Cdebug::n=1;赋值了,它就不仅仅是一个声明了,而是实现,如果把这一句放在cpp文件里就好了。
你把你的定义放在头文件中,然后在实现的cpp文件在进行赋值。
private:
static int n;
}
int Cdebug::n=1;
#endif
重点,要考
去掉int
int Cdebug::n=1;这句去掉int
建议你把这句话写到类的实现文件中
int Cdebug::n=1;写到cpp文件中
然后在h文件中添加extern int Cdebug::n=1;