重复符号错误C ++ [英] duplicate symbol error C++

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

问题描述

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

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 char* xyz = "xyz";
class Abc
{
public:
    Abc()
    {
    }
};


推荐答案

如果这是一个头文件,每次 #include 时定义 xyz

If this is in a header file, you're defining xyz every time you #include it.

你可以改变声明为@R塞缪尔·克拉奇科显示。通常的方式(如果数据不是 const )是这样的:

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

在Abc.h: / p>

In Abc.h:

extern char *xyz;

在Abc.cpp中:

char *xyz = "xyz";

已编辑以添加

请注意,标题防护将解决此问题:

Note that header guards will not solve this problem:

#ifndef XYZ_H
#define XYZ_H
...
#endif

redefinition错误,其中相同的符号在同一编译单元中出现两次。这是一个编译器错误。

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

但是,即使使用标题保护, xyz 仍会出现在包含它的每个源文件中,从而导致重复符号错误,这是一个链接器错误。

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

如果原来的海报提到,更有帮助,当然。

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

这篇关于重复符号错误C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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