问题描述
我有一个名为 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_HEIGHTHowever when I do this I get the following error:
错误 6 错误 LNK2001: 未解析的外部符号 "public: static双酒馆::Y_FACTOR"(?Y_FACTOR@Pub@@2NA) C:\Users\Pedro-Estevan-Juarez\Documents\Visual Studio2012\Projects\Project2\Project2\main.obj Project2 错误 7 错误LNK1120:1 个未解析的外部文件 C:\Users\Pedro-Estevan-Juarez\Documents\VisualStudio 2012\Projects\Project2\Debug\Project2.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;
这篇关于带有静态变量的未解析的外部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!