符号是乘法定义的 [英] symbol is multiply defined

查看:229
本文介绍了符号是乘法定义的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.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 error ld: 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.

这篇关于符号是乘法定义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆