在非静态函数中比较静态和非静态整数

编程入门 行业动态 更新时间:2024-10-10 10:33:01
本文介绍了在非静态函数中比较静态和非静态整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个静态变量,我用作计数器和非静态版本的变量,我用来保存计数器的值在某些事件。下面是一些代码:

I have a static variable that I use as a counter and a non-static version of the variable that I use to save the value of the counter at certain events. Here is some code:

头文件:

static int UndoID; int UndoRedoID; void SetUnsavedChanges();

类:

我尝试这样的类:

UndoRedoID = UndoID;

我尝试过其他的操作:

UndoRedoID = myClass:UndoID;

比较示例:

void myClass::SetUnsavedChanges() { if (UndoRedoID != UndoID) { cout << "Unsaved"; } else { cout << "Saved"; } }

这导致我得到链接错误, p>

This causes me to get linking errors like:

Undefined symbols: "myClass::UndoID", referenced from: myClass::SetUnsavedChanges() in myClass_lib.a(myClass.o) ...

感谢您的帮助: )

推荐答案

您需要定义静态成员数据, >

You need to define the static member data, outside the class as:

//this should be done in .cpp file int myClass::UndoID;

让我添加一个例子:

//X.h class X { static int s; //declaration of static member };

然后在 X.cpp 应该这样做:

//X.cpp #include "X.h" int X::s; //definition of the static member

更多推荐

在非静态函数中比较静态和非静态整数

本文发布于:2023-10-31 06:43:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   整数   函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!