2017-04-19 4 views
1

특정 생성자를 사용하여 생성 된 개체의 개수를 계산하려고합니다. 난 C# 같은 언어로 이렇게 전에 static int 내가 생성자에서 증가 해요 변수를 만들었습니다. 코드 여기C++ 정적 변수 선언 이상한 링커 오류

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

됩니다 :

나는이 라인을 포함하고있어 경우
#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 오류가 발생했을 :

- 내가 당신에게 코드를 표시하기 전에

여기 compilement 오류입니다

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;