1
我想計算已使用某個構造函數創建的對象的數量。我之前在C#
之類的語言中這樣做過,所以我創建了一個static int
變量,我在構造函數中遞增。C++靜態變量聲明奇怪的鏈接器錯誤
之前,我告訴你的代碼 - 這裏是compilement錯誤:
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錯誤occurrs如果我包括這行:
Bill b(1, 2, 3);