重复符号错误 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2894957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:25:39  来源:igfitidea点击:

duplicate symbol error C++

c++

提问by boom

I have added some const character in my file as under. The error i get is duplicate symbol _xyz(say). What is the problem with it and how could i get out of this.

我在我的文件中添加了一些 const 字符,如下所示。我得到的错误是重复符号 _xyz(say)。它有什么问题,我怎么能摆脱这个。

const char* xyz = "xyz";
class Abc
{
public:
    Abc()
    {
    }
};

回答by egrunin

If this is in a header file, you're defining xyzevery time you #includeit.

如果这是在头文件中,则xyz每次都在定义#include

You can change the declaration as @R Samuel Klatchko shows. The usual way (if the data isn't const) is like this:

您可以像@R Samuel Klatchko 显示的那样更改声明。通常的方式(如果数据不是const)是这样的:

In Abc.h:

在 Abc.h 中:

extern char *xyz;

In Abc.cpp:

在 Abc.cpp 中:

char *xyz = "xyz";

Edited to add

编辑添加

Note that header guards will notsolve this problem:

请注意,header guards不会解决这个问题:

#ifndef XYZ_H
#define XYZ_H
...
#endif

Header guards prevent "redefinition" errors, where the same symbol appears twice in the same compilation unit. That's a compilererror.

标头保护可防止“重新定义”错误,即同一符号在同一编译单元中出现两次。那是编译器错误。

But even with header guards the definition of xyzwill still appear in every source file that includes it, causing a "duplicate symbol" error, which is a linkererror.

但即使有头文件保护, 的定义xyz仍然会出现在包含它的每个源文件中,导致“重复符号”错误,这是一个链接器错误。

It would have been more helpful if the original poster had mentioned that, of course.

当然,如果原始海报提到这一点会更有帮助。

回答by R Samuel Klatchko

The problem is every source file that includes your header file gets it's own copy of xyzwith external linkage.

问题是包含您的头文件的每个源文件都有自己的xyz带有外部链接的副本。

The easiest way to fix that is to give xyzinternal linkage. You can do that by making the pointer itself const in addition to having the underlying char's const:

解决这个问题的最简单方法是提供xyz内部链接。除了具有底层 char 的 const 之外,您还可以通过使指针本身成为 const 来做到这一点:

const char* const xyz = "xyz";

回答by Eric Wiener

I also ran into this issue, but for me the solution was different. I had put overloaded operators (==, !=, <<) in my header file and implemented them. This was causing an issue in other files where I also used ==, !=, or <<. To solve this, I moved the implementation into the .cpp file and left the declaration in the header file.

我也遇到了这个问题,但对我来说解决方案是不同的。我在头文件中放置了重载运算符(==、!=、<<)并实现了它们。这在我也使用 ==、!= 或 << 的其他文件中引起了问题。为了解决这个问题,我将实现移到 .cpp 文件中,并将声明留在头文件中。

回答by AnT

Please, provide a meaningful description of the problem. What's "my file"? What "const character" are you taking about?

请提供对问题的有意义的描述。什么是“我的文件”?你在谈论什么“常量字符”?

For what you provided so far I can only guess that you added the above definition of xyzto a header file and then included it into several translation units. The result: object xyzgot defined more than once. Hence the error.

对于您目前提供的内容,我只能猜测您将上述定义添加xyz到头文件中,然后将其包含到多个翻译单元中。结果:对象xyz被定义了不止一次。因此错误。