class 中未解析的外部符号
unresolved external symbol in class
我已经尝试了几个小时来消除我的错误,但它不会消失,我已经查看了其他帖子,但似乎没有任何效果。
我的 .H 文件是这样的:
using namespace std;
class common
{
public:
common();
static double common::s_a;
static double common::s_b;
然后我得到一个 .CPP 文件,我在其中定义了这样的变量:
#include "common.h"
common::common()
{
common::s_a = 100;
common::s_b = 100;
}
然后我收到这个错误消息(实际变量名而不是 a)
common.obj : error LNK2001: unresolved external symbol "public: static
double common::s_playerMaxHealth" (?s_playerMaxHealth@common@@2NA)
编辑: 问题是静态的,如果我删除静态的,我就不会再有错误了。但是我需要使用 static 才能按预期工作。
您必须像这样定义这些变量(在您的 .cpp 文件中,在任何函数之外):
double common::s_a;
double common::s_b;
这是声明(不是定义):
class common
{
static double common::s_a;
static double common::s_b;
这是一个用途(也不是定义):
common::common()
{
common::s_a = 100;
common::s_b = 100;
}
我已经尝试了几个小时来消除我的错误,但它不会消失,我已经查看了其他帖子,但似乎没有任何效果。
我的 .H 文件是这样的:
using namespace std;
class common
{
public:
common();
static double common::s_a;
static double common::s_b;
然后我得到一个 .CPP 文件,我在其中定义了这样的变量:
#include "common.h"
common::common()
{
common::s_a = 100;
common::s_b = 100;
}
然后我收到这个错误消息(实际变量名而不是 a)
common.obj : error LNK2001: unresolved external symbol "public: static double common::s_playerMaxHealth" (?s_playerMaxHealth@common@@2NA)
编辑: 问题是静态的,如果我删除静态的,我就不会再有错误了。但是我需要使用 static 才能按预期工作。
您必须像这样定义这些变量(在您的 .cpp 文件中,在任何函数之外):
double common::s_a;
double common::s_b;
这是声明(不是定义):
class common
{
static double common::s_a;
static double common::s_b;
这是一个用途(也不是定义):
common::common()
{
common::s_a = 100;
common::s_b = 100;
}