Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Initialisierung einer statischen Variable in einer Klasse

Empfohlene Antworten

Hallo,

mein Problem ist folgendes: Ich benutze in meinem Programm eine Klasse, in deren privaten Deklarationsbereich eine statische Variable (ein STL-vector) deklariert wird. Sobald ich dann allerdings versuche, den vector beispielsweise über resize() zu verändern, beschwert sich der Linker über ein nicht aufgelöstes externes Symbol. Über die Initialisierungsliste geht es wegen dem static leider nicht...

(...)


class abc

{

private:

	int money, sMoney, rWCount, rLCount;

	static vector < vector<int> > stats;

public:

	void setStartMoney(int);

	void writeStats();

	int getWC();

	int getLC();

	int getMoney();

	int getStartMoney();

	int Play(unsigned long, int);

	abc();

};


(...)


abc::abc()

:rWCount(0), rLCount(0)

{

	stats.resize(5);  //<- geht nicht

}

Wenn ich in einer anderen Methode der Klasse auf stats zugreifen will, habe ich das gleiche Problem.

Kann mir jemand sagen was ich falsch mache?

In deinem Code fehlt die Definition der Variablen. Das, was da in der Klassendefinition steht, ist nämlich nur eine Deklaration.

Man muss statische Member auch noch zusätzlich in einer Übersetzungseinheit (.cpp-Datei) definieren, dort kann man dann auch gleich eine Initialisierung vornehmen:

vector < vector<int> > abc::stats(5);

Danke, jetzt gehts...

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Konto

Navigation

Suchen

Suchen

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.