c++ - 静态变量访问

标签 c++

<分区>

Possible Duplicate:
Access issue regarding static variable

我遇到了一个看似微不足道的问题,但我似乎无法找出原因。

我有一个叫做存储的类。 头文件:

#include <string>
using namespace std;
#include "Player.h"

class Storage {
public:
    static void Initialise();
    static string GetInformation();     
private:
    static Player player;
};

CPP 文件:

string Storage::GetInformation() {
    string returnString = "";
    // Get the players money
    // Convert it to a string
    string money("money");
    stringstream out;
    out << player.GetMoney();
    money = out.str();
    returnString += "Money: " + money + "\n";

    // Get the players ship information
    returnString += player.GetShipInformation();

    // Get the players current location
    returnString += player.GetCurrentLocation();

    return returnString;
}

void Storage::Initialise() {

}

这给出了一个错误:“对 `Storage::player' 的 undefined reference ”。我试过用谷歌搜索它并进行调整,但我似乎找不到任何有用的东西。如果有人能为我指出正确的方向让我看一篇文章,那就太好了,因为我不确定要搜索什么术语才能获得正确的答案。

最佳答案

您已声明该成员,但尚未定义它。

例如,您需要Storage.cpp 在最外层,即与方法定义在同一层:

Player Storage::player;

关于c++ - 静态变量访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12567823/

相关文章:

c++ - Boost.Geometry 的 GIS 扩展

c++ - 文件处理 C++ 时出错

C++ 字符串到枚举

c++ - 估计分数的概率( pig )C++

c++ - 在类成员函数内累积同一类的对象

c++ - 使用三角形带将三角形移动到新列

.net - 从非特定的 .NET 应用引用特定于平台的库

用于定义公共(public)变量 static const integer 的 C++ 标准

c++ - 将模板参数加为好友

c++ - 奇怪的循环情况,不知道是什么原因造成的