0

カウンターとして使用する静的変数と、特定のイベントでカウンターの値を保存するために使用する非静的バージョンの変数があります。ここにいくつかのコードがあります:

ヘッダ:

static int UndoID;
int UndoRedoID;

void SetUnsavedChanges();

クラス:

クラスのさまざまな部分で、私は次のようなことを試みます。

UndoRedoID = UndoID;

私は次のような他のことを試しました:

UndoRedoID = myClass:UndoID;

比較例:

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

これにより、次のようなリンクエラーが発生します。

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

ご協力ありがとうございました :)

4

1 に答える 1

2

クラスの外部で、静的メンバーデータを次のように定義する必要があります。

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

1つの例を追加しましょう:

//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
于 2012-03-29T15:21:17.120 に答える