C++类静态变量问题 - C 程序员新手 C++

发布于 2024-08-31 04:57:10 字数 743 浏览 2 评论 0原文

我是一名 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拥抱影子 2024-09-07 04:57:10

静态变量需要在类体之外的某个地方定义。类体内的声明只是一个声明。

例如,在全局范围内:

int _filter_session::session_count;

您需要确保此定义在程序中仅出现一次,因此通常您会将其放置在源文件(.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:

int _filter_session::session_count;

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 to typedef your class name. class Name { //... introduces a type name in C++, you wouldn't have to use class Name to refer to the type.

热情消退 2024-09-07 04:57:10

与你的问题无关,但在 C++ 中,不需要像这样键入定义类和结构:

typedef class _filter_session
{
  ...
}FILTER_SESSION;

你可以而且应该简单地说:

class filter_session
{
  ...
};

此外,c;lass 名称通常不应为大写,因为人们会将它们与宏混淆。并且您很少需要使用 this-> 构造 - 您的代码当然不需要。

Not to do with your problem, but in C++ there is no need to typedef classes and structs like this:

typedef class _filter_session
{
  ...
}FILTER_SESSION;

You can and should simply say:

class filter_session
{
  ...
};

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.

梦断已成空 2024-09-07 04:57:10

您需要初始化静态变量。这段代码实际上编译:

typedef class _filter_session
{
private:
    static int session_count;  /* Number of sessions count -- Static */    
public:
    _filter_session();         /* Constructor */
    ~_filter_session();        /* Destructor */
}; // FILTER_SESSION;

int _filter_session::session_count = 0;


_filter_session::_filter_session(void)
{
    session_count++;
    return;
}


_filter_session::~_filter_session(void)
{
    session_count--;
    return;
}

int main(int argc, const char **argv)
{
  return 0;
}

注意,我注释了 FILTER_SESSION 在 g++/Linux 上编译,并且还添加了一个 main 并删除了 this->; (正如另一位成员提到的,变量不是对象的属性,而是类的属性。将其视为命名空间全局变量)

You need to initialize the static variable. This code actually compiles:

typedef class _filter_session
{
private:
    static int session_count;  /* Number of sessions count -- Static */    
public:
    _filter_session();         /* Constructor */
    ~_filter_session();        /* Destructor */
}; // FILTER_SESSION;

int _filter_session::session_count = 0;


_filter_session::_filter_session(void)
{
    session_count++;
    return;
}


_filter_session::~_filter_session(void)
{
    session_count--;
    return;
}

int main(int argc, const char **argv)
{
  return 0;
}

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)

靖瑶 2024-09-07 04:57:10

只需使用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 the this-pointer. All instances of your class share one instance of session_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文