【问题标题】:Unresolved external, trying to use static variable c++ [duplicate]未解决的外部,试图使用静态变量c ++ [重复]
【发布时间】:2017-03-13 21:02:11
【问题描述】:

我在 world.h 中有 World 类:

class World
{
public:
    static Ground* ground;
};

在函数的另一个类中,我尝试像这样使用静态变量 ground:

#include "Node.h"
#include "World.h"
void Node::Foo()
{
    Ground* ground = World::ground;
}

在 world.cpp 中我也有:

#include "stdafx.h"
#include "World.h"

static Ground* ground = new Ground(10, 10);

但我收到以下错误:

  • LNK2001 未解析的外部符号“public: static class Ground World::ground”(?ground@World@@2PAVGround@@A)
  • LNK1120 1 未解决的外部问题

【问题讨论】:

  • 请将错误信息作为文本,而不是图像
  • static Ground* ground = new Ground(10, 10); -> Ground* World::ground = new Ground(10, 10);
  • 这个问题可能是重复的,但它的措辞比原来的问题要清楚得多。 +1 为 OP。

标签: c++ class


【解决方案1】:
static Ground* ground = new Ground(10, 10);

你在那里缺少World::,所以你定义了一个完全不相关的变量,它恰好具有相同的名称。你应该有这个:

Ground* World::ground = new Ground(10, 10);

【讨论】:

  • 我确实必须添加World::,并且我还必须从cpp文件中删除static
  • 好点。我编辑了我的答案以反映这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多