<分区>
我想计算使用某个构造函数创建的对象的数量。我以前用 C#
这样的语言做过这个,所以我创建了一个 static int
变量,我在构造函数中递增它。
在我向您展示代码之前 - 这是编译错误:
Severity Code Description Project File Line Suppression State Error LNK2001 unresolved external symbol "private: static int Bill::count_of_created_bills" (?count_of_created_bills@Bill@@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;
}
};
如果我包含此行,则会发生编译错误:
账单 b(1, 2, 3);