在 XCode 中定义静态类变量时出现 C++ 重复符号错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1858571/
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
C++ Duplicate Symbol error when defining static class variable in XCode
提问by Old McStopher
I have a static class member incremented in the constructor. As per the rules, it is declared in the class and defined outside. This should be totally legal. Any ideas why I'm getting a duplicate symbol error?
我在构造函数中增加了一个静态类成员。根据规则,它在类中声明并在外部定义。这应该是完全合法的。任何想法为什么我收到重复的符号错误?
class Player
{
private:
static int numPlayers;
public:
Player() { numPlayers++; }
};
int Player::numPlayers = 0;
回答by Michael Aaron Safyan
The problem is that you are not separating your DECLARATION from your DEFINITION. Consider:
问题是您没有将您的声明与您的定义分开。考虑:
class Player { private: static int numPlayers; public: Player() { numPlayers++; } };
The code above merely declares the existence of "numPlayers" in the class "Player". It does not, however, reserve any space or assign a memory address to the variable "Player::numPlayers". However:
上面的代码只是声明了“Player”类中“numPlayers”的存在。但是,它不会为变量“Player::numPlayers”保留任何空间或分配内存地址。然而:
int Player::numPlayers = 0;
The code above is a DEFINITION -- it reserves space for the object Player::numPlayers and designates a unique address for that object. Having that line appear more than once in a program violates the one-definition-rule. Now what is most likely happening is that you are including this file...
上面的代码是一个定义——它为对象 Player::numPlayers 保留空间并为该对象指定一个唯一的地址。在程序中多次出现该行违反了单一定义规则。现在最有可能发生的是您正在包含此文件...
You should NEVER, EVER include a ".c", ".cpp", ".m", ".mm" or any other "source" file (i.e. a file that contains DEFINITIONS). You should only include "header" files (i.e. files containing purely DECLARATIONS). For many build systems, including Xcode, each source file is automatically compiled and linked into the project. If you include a source file from another source file, then the definitions get linked in twice -- first when it is compiled on its own and then again when it is referenced by another source file.
您永远不应该包含“.c”、“.cpp”、“.m”、“.mm”或任何其他“源”文件(即包含定义的文件)。您应该只包含“头”文件(即包含纯粹声明的文件)。对于包括 Xcode 在内的许多构建系统,每个源文件都会自动编译并链接到项目中。如果您包含来自另一个源文件的源文件,那么定义会被链接两次——第一次是在它自己编译时,然后是在它被另一个源文件引用时。
Since you are asking about Xcode... you can remedy this issue by unchecking the source file in the project detail view; a check mark next to a source file indicates that it will be compiled and linked-in for the current target. However, I strongly suggest that you leave your ".mm" file checked, create a ".h" file in which you put your declarations, and include the ".h" file instead of including one source file from another.
由于您是在询问 Xcode……您可以通过在项目详细信息视图中取消选中源文件来解决此问题;源文件旁边的复选标记表示它将为当前目标编译和链接。但是,我强烈建议您选中“.mm”文件,创建一个“.h”文件来放置您的声明,并包含“.h”文件而不是包含另一个源文件。
回答by aJ.
Did you define multiple times ? ie define it in header file and include in multiple cpp files.
你定义了多次吗?即在头文件中定义它并包含在多个 cpp 文件中。