C++在 XCode 中定义静态类变量时出现重复符号错误

发布于 2024-08-13 11:03:21 字数 232 浏览 2 评论 0原文

我有一个在构造函数中递增的静态类成员。根据规则,它在类中声明并在外部定义。这应该是完全合法的。有什么想法为什么我会收到重复符号错误吗?

class Player
{
   private:
      static int numPlayers;
   public:
      Player() { numPlayers++; }
};

int Player::numPlayers = 0;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆萌少年 2024-08-20 11:03:21

问题是您没有将声明与定义分开。考虑一下:

class Player
{
   private:
      static int numPlayers;
   public:
      Player() { numPlayers++; }
};

上面的代码只是声明类“Player”中存在“numPlayers”。但是,它不会保留任何空间或为变量“Player::numPlayers”分配内存地址。然而:

int Player::numPlayers = 0;

上面的代码是一个定义——它为对象 Player::numPlayers 保留空间并为该对象指定一个唯一的地址。让该行在程序中出现多次违反了单一定义规则。现在最有可能发生的情况是您包含此文件...

您永远不应该包含“.c”、“.cpp”、“.m”、“.mm”或任何其他“源”文件(即包含定义的文件)。您应该只包含“头”文件(即包含纯粹声明的文件)。对于许多构建系统(包括 Xcode),每个源文件都会自动编译并链接到项目中。如果您包含来自另一个源文件的源文件,则定义会链接两次 - 第一次是在它自己编译时,然后是当它被另一个源文件引用时。

既然您询问的是 Xcode...您可以通过取消选中项目详细信息视图中的源文件来解决此问题;源文件旁边的复选标记表示它将针对当前目标进行编译和链接。但是,我强烈建议您选中“.mm”文件,创建一个在其中放置声明的“.h”文件,并包含“.h”文件,而不是包含另一个源文件。

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:

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...

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.

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.

数理化全能战士 2024-08-20 11:03:21

你定义了多次吗?即在头文件中定义它并包含在多个cpp文件中。

Did you define multiple times ? ie define it in header file and include in multiple cpp files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文