问题描述
我有一个.h档案
test.h
std :: list< std :: string> RegisterList;
我想在2个.c文件中访问此列表
Ac
#includetest.h
RegisterList.push_back );
Bc
c $ c> #includetest.h
RegisterList.push_back(myString2);
当我编译这个代码时,我得到一个错误
ld:fatal:symbol'RegisteredList '是乘法定义的:
问题是什么?默认情况下,在test.h中初始化RegsiterList是导致此问题的原因?
文件并违反了一个定义规则。不要在头文件中定义对象。
如果要使用全局对象,则需要将其声明为extern并将其定义在一个且只有一个源文件中。 / p>
I have a .h file
test.h
std::list<std::string> RegisterList;
I want to access this list in 2 .c files
A.c
#include "test.h"
RegisterList.push_back(myString);
B.c
#include "test.h"
RegisterList.push_back(myString2);
When i compile this code, i get an errorld: fatal: symbol `RegisteredList' is multiply-defined:
What can be the problem ? Is RegsiterList is initialized by default in test.h which is leading to this problem ?
Because you defined the object in the header file and violated one definition rule. Never define objects in header file.
If you want to use a global object, You need to declare it as extern and define it in one and only one source file.
这篇关于符号是乘法定义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!