2017-12-01 12 views
0

私はC++を初めて使用し、クラスメンバーおよび関数の静的機能を調べています。静的メンバー関数が静的専用変数にアクセスするときのリンカーエラー

person.h

#include <iostream> 

using namespace std; 

namespace school 
{ 

class Person 
{ 

    public: 
     static const int MAX=99; 

    private:  
     static int idcount; 

    public: 
     static void setID(int id) { idcount = id;} 
     static int getID() { return idcount;} 

    private: 
     string name;  

    public: 
     Person(); 
     Person(const Person &other); 
     ~Person(); 


}; 

} 

Person.cpp

#include "person.h" 

namespace school 
{ 

    //Constructor 
    Person::Person() 
    { 
     this->name = ""; 
     cout << "object created" << endl; 
    } 

    //Copy Constructor 
    Person::Person(const Person &other) 
    { 
     this->name = other.name; 
    } 

    //Destructor 
    Person::~Person() 
    { 
     cout << "Destructor Called" << endl; 
    } 
} 

main.cppに

#include "person.h" 

int main() 
{ 


    cout << school::Person::MAX << endl; 
    school::Person::setID(5); 
    cout << school::Person::getID() << endl; 

    return 0; 
} 

私は上記のコードをコンパイルするときに、私は以下のリンカエラーを取得しています。しかし、私はidcountをpublicに変更し、それをmain(int Person :: idcount;)と宣言すると問題はありません。

D:\Hari\Project\CPP_Practise\build>mingw32-make 
[ 50%] Built target person 
Scanning dependencies of target app 
[ 75%] Building CXX object CMakeFiles/app.dir/chapter2/classes2.cpp.obj 
[100%] Linking CXX executable app.exe 
CMakeFiles\app.dir/objects.a(classes2.cpp.obj): In function 
`ZN6school6Person5se 
tIDEi': 
D:/Hari/Project/CPP_Practise/chapter2/person.h:21: undefined reference to 
`schoo 
l::Person::idcount' 
CMakeFiles\app.dir/objects.a(classes2.cpp.obj): In function 
`ZN6school6Person5ge 
tIDEv': 
D:/Hari/Project/CPP_Practise/chapter2/person.h:22: undefined reference to 
`schoo 
l::Person::idcount' 
collect2.exe: error: ld returned 1 exit status 
CMakeFiles\app.dir\build.make:97: recipe for target 'app.exe' failed 
mingw32-make[2]: *** [app.exe] Error 1 
CMakeFiles\Makefile2:103: recipe for target 'CMakeFiles/app.dir/all' 
failed 
mingw32-make[1]: *** [CMakeFiles/app.dir/all] Error 2 
Makefile:82: recipe for target 'all' failed 
mingw32-make: *** [all] Error 2 

プライベート静的変数として使用するときはどうすればよいですか?

+1

これを 'private'に変更する必要はありません。単に' Person :: idcount; 'として定義してください。これは' Person.cpp'の方が良いでしょう。 – songyuanyao

+0

あなたはエラーを表示していませんが、私はそれがこの回答の一番下に隠れていると推測します:https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external -symbol-error-and-how-do-i-fix/12574407#12574407 – chris

+0

yes person.cppに追加する - エラーを解決しますが、その背後にある理論は何か不思議です。だから、すべての静的int、privateまたはpublicはクラス定義で宣言する必要がありますか? – hariudkmr

答えて

0

1.静的変数は、そのクラスに対して作成されたすべてのオブジェクトに共通することを意味します。 2.ヘッダファイルに書き込まれたものは、青写真として機能します。つまり、メモリに割り当てられていません。このため、すべての静的変数はcpp実装ファイルで定義されています。コンパイラは、cppファイルで定義されたメモリにメモリを割り当てます。

+0

プライベートでも公的でもかまいませんか? – hariudkmr

+0

プライベート/パブリック変数であれば問題ありません。そのクラスの静的メンバーだけがプライベート変数にアクセスできます。必要に応じてサンプルアプリを試すことができます。 –

0

Person :: iは依然として(クラス本体の外側で)定義する必要があります。 int Person :: i;

体内の値が割り当てられていないスタティックだけを外側に定義する必要があります。そして、不揮発性定数積分だけが、そのような体内割り当てを有することができる。

+0

ビットを拡張する - 静的変数以外の静的変数は、プライベートかパブリックかをアクセスするファイルで宣言する必要がありますか? – hariudkmr

関連する問題