在非静态函数中比较静态和非静态整数 [英] Compare static and non-static integer in non-static function

查看:168
本文介绍了在非静态函数中比较静态和非静态整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于在非静态函数中比较静态和非静态整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆