2017-04-19 9 views
1

特定のコンストラクタを使用して作成されたオブジェクトの数をカウントします。私はC#のような言語でこれをやっています。その前にコンストラクタでインクリメントしている変数static intを作成しました。C++静的変数宣言不自然なリンカーエラー

私はあなたのコードを表示する前に - ここcompilementエラーです:

#pragma once 
class Bill 
{ 
private: 
    static int count_of_created_bills; 
    int id;    // Unique Identification 
    double ammount;  // Ammount of bill  
    int month;   // Month of bill (January = 0, February = 1 ...) 
    int type_of_spending; // Type of spending (Food = 0 ...) 
public: 
    Bill(int a, int m, int t):ammount(a), month(m), type_of_spending(t) 
    { 
     count_of_created_bills++; 
     id = count_of_created_bills; 
    } 
}; 

私は、この行を含めていた場合compilementエラーがoccurrs:

ここ

Severity Code Description Project File Line Suppression State Error LNK2001 unresolved external symbol "private: static int Bill::count_of_created_bills" ([email protected]@@0HA) Ausgabenverwaltung c:\Users\xy\documents\visual studio 2017\Projects\Ausgabenverwaltung\Ausgabenverwaltung\Ausgabenverwaltung.obj 1

コードです

Bill b(1, 2, 3);

答えて

1

あなたは初期化を追加するのを忘れています:

Bill::Bill(int a, int m, int t):ammount(a), month(m), type_of_spending(t) 
    { 

     std::cout << "::Ros-App!" << Foo::count_of_created_bills << std::endl; 
     Foo::count_of_created_bills++; 
     id = count_of_created_bills; 
    } 
int Bill::count_of_created_bills = 0; 
関連する問題