C++类静态变量问题 - C 程序员新手 C++
我是一名 C 程序员,但在 @school 学习过 C++ 很久了。现在我尝试用 C++ 编写代码,但出现编译器错误。请检查并告诉我我的代码有什么问题。
typedef class _filter_session
{
private:
static int session_count; /* Number of sessions count -- Static */
public:
_filter_session(); /* Constructor */
~_filter_session(); /* Destructor */
}FILTER_SESSION;
_filter_session::_filter_session(void)
{
(this->session_count)++;
return;
}
_filter_session::~_filter_session(void)
{
(this->session_count)--;
return;
}
我收到的错误是
“错误 LNK2001: 无法解析的外部符号“private: static int _filter_session::session_count” (?session_count@_filter_session@@0HA)”
顺便说一下,我正在使用 Visual Studio 2005。
请帮助我。
问候,
微内核
I am a C programmer, but had learnt C++ @school longtime back. Now I am trying to write code in C++ but getting compiler error. Please check and tell me whats wrong with my code.
typedef class _filter_session
{
private:
static int session_count; /* Number of sessions count -- Static */
public:
_filter_session(); /* Constructor */
~_filter_session(); /* Destructor */
}FILTER_SESSION;
_filter_session::_filter_session(void)
{
(this->session_count)++;
return;
}
_filter_session::~_filter_session(void)
{
(this->session_count)--;
return;
}
The error that I am getting is
"error LNK2001: unresolved external symbol "private: static int _filter_session::session_count" (?session_count@_filter_session@@0HA)"
I am using Visual Studio 2005 by the way.
Plz plz help me.
Regards,
Microkernel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
静态
变量需要在类体之外的某个地方定义。类体内的声明只是一个声明。例如,在全局范围内:
您需要确保此定义在程序中仅出现一次,因此通常您会将其放置在源文件(
.cc
或.cpp
)中,并且不是包含在多个翻译单元中的头文件。为了可移植性,您应该避免以
_
开头的类名。也几乎不需要typedef
你的类名。class Name { //...
在 C++ 中引入了类型名称,您不必使用class Name
来引用该类型。static
variables need to be defined outside of the class body somewhere. The declaration inside the class body is just a declaration.E.g. at global scope:
You need to ensure that this definition occurs only once in the program so usually you would place it in a source file (
.cc
or.cpp
) and not a header file which is included in more than once translation unit.For portability you should avoid class names that start with an
_
. There is also little need totypedef
your class name.class Name { //...
introduces a type name in C++, you wouldn't have to useclass Name
to refer to the type.与你的问题无关,但在 C++ 中,不需要像这样键入定义类和结构:
你可以而且应该简单地说:
此外,c;lass 名称通常不应为大写,因为人们会将它们与宏混淆。并且您很少需要使用
this->
构造 - 您的代码当然不需要。Not to do with your problem, but in C++ there is no need to typedef classes and structs like this:
You can and should simply say:
Also, c;lass names should not normally be in uppercase, as people will confuse them with macros. and you rarely need to use the
this->
construct - your code certainly does not.您需要初始化静态变量。这段代码实际上编译:
注意,我注释了 FILTER_SESSION 在 g++/Linux 上编译,并且还添加了一个 main 并删除了 this->; (正如另一位成员提到的,变量不是对象的属性,而是类的属性。将其视为命名空间全局变量)
You need to initialize the static variable. This code actually compiles:
Note, I commented FILTER_SESSION to compile on g++/Linux, and also added a main and removed the this-> (as another member mentions, the variable is not a property of the object, but of the class. Think it as a namespaced global variable)
只需使用
session_count++
。静态变量不绑定到类的任何实例,因此不能通过this
指针访问它。类的所有实例共享一个session_count
实例。事实上,即使您的类的实例不存在,也可以访问session_count
。编辑好吧,我的回答没有解决问题,但是查尔斯·贝利的确实如此。
Just use
session_count++
. A static variable is not tied to any instance of a class, and hence it cannot be accessed through thethis
-pointer. All instances of your class share one instance ofsession_count
. In fact,session_count
can be accessed even if no instances of your class exist.Edit Ok, my answer does not solve the problem, but Charles Bailey`s does.