未解决的外部静态变量
我有一个名为 Pub 的类,它具有以下标题:
I have a class called Pub which has the following header:
#pragma once
class Pub
{
public:
static double X_FACTOR;
static double Y_FACTOR;
static const int INIT_SCREEN_WIDTH=500;
static const int INIT_SCREEN_HEIGHT=550;
Pub(void);
~Pub(void);
};
我正在尝试使用以下内容在 main.cpp 中设置变量 Y_FACTOR:
I am trying to set the variable Y_FACTOR in main.cpp with the following:
Pub::Y_FACTOR=1.0;
是的,Pub.h 已正确包含,这可以证明我可以访问 INIT_SCREEN_WIDTH 和 INIT_SCREEN_HEIGHT但是,当我这样做时,会出现以下错误:
and yes Pub.h is included properly which can be demonstrated as I can access INIT_SCREEN_WIDTH and INIT_SCREEN_HEIGHT However when I do this I get the following error:
错误 6 错误 LNK2001: 无法解析的外部符号 "public: static双 Pub::Y_FACTOR"(?Y_FACTOR@Pub@@2NA) C:UsersPedro-Estevan-JuarezDocumentsVisual Studio2012ProjectsProject2Project2main.obj Project2 错误 7 错误LNK1120:1 个未解决的外部 C:UsersPedro-Estevan-JuarezDocumentsVisualStudio 2012ProjectsProject2DebugProject2.exe 1 1 Project2
Error 6 error LNK2001: unresolved external symbol "public: static double Pub::Y_FACTOR" (?Y_FACTOR@Pub@@2NA) C:UsersPedro-Estevan-JuarezDocumentsVisual Studio 2012ProjectsProject2Project2main.obj Project2 Error 7 error LNK1120: 1 unresolved externals C:UsersPedro-Estevan-JuarezDocumentsVisual Studio 2012ProjectsProject2DebugProject2.exe 1 1 Project2
我怀疑这是语法方面的问题,有人可以帮我解决这个问题吗?
I suspect this is something syntax wise, can someone please help me with this?
推荐答案
类定义中的代码只是一个声明.您需要在 cpp 文件中添加静态变量的定义.在使用它的任何函数之前,将其添加到您的 cpp 文件和文件范围内.
The code inside the class definition is just a declaration. You need to add definition of the static variable in a cpp file. Add this in your cpp file and in file scope before any function using it.
double Pub::Y_FACTOR;
相关文章