Problem Static Pointer

Du bist neu in der Welt von C++? Dann schau hier herein!
Antworten
xchrisx
Beiträge: 18
Registriert: 23. August 2007 20:20
Wohnort: Duisburg
Kontaktdaten:

Problem Static Pointer

Beitrag von xchrisx »

Hallo Zusammen.
Ich stehe gerade auf dem Schlauch.
Ich nutze das State Pattern der GoF und will die Zustände als Singelton implementieren.
Leider wird mir beim Linken ein Fehler ausgeworfen.
Meine Spezielle Zustandsklasse:

Code: Alles auswählen

#include "DCMotorState.h"


class DCMotorStateClosing : public DCMotorState {
public:
	static DCMotorStateClosing* pInstance;
	static DCMotorState* Instance();

	DCMotorStateClosing();
	~DCMotorStateClosing();
	void open(DCMotor* motor);
	void checkTime(DCMotor* motor);
	void raiseError(DCMotor* motor);
	void closingPositionDetected(DCMotor* motor);
private:
	
};
Die Implementierung der statischen Funktion:

Code: Alles auswählen

DCMotorState* DCMotorStateClosing::Instance(){
	if(DCMotorStateClosing::pInstance == NULL)
	{
		DCMotorStateClosing::pInstance = new DCMotorStateClosing();
	}
	return DCMotorStateClosing::pInstance;
}
Und die Fehlermeldung:
Linking...
1>DCMotorStateClosing.obj : error LNK2001: unresolved external symbol "public: static class DCMotorStateClosing * DCMotorStateClosing::pInstance" (?pInstance@DCMotorStateClosing@@2PAV1@A)
1>C:\Users\s\Documents\Visual Studio 2008\Projects\uCMotorTest1\Debug\uCMotorTest1.exe : fatal error LNK1120: 1 unresolved externals

Ev sieht jemand den Fehler :)
solarix
Beiträge: 1133
Registriert: 7. Juni 2007 19:25

Beitrag von solarix »

Da fehlt noch die Definition der statischen Variabel in der cpp-File:

So in etwa:

Code: Alles auswählen

DCMotorStateClosing* DCMotorStateClosing::pInstance = NULL;
btw: Beim Singleton-Pattern macht es keinen Sinn, "pInstance" und der Konstruktor als "public" zu deklarieren....

hth!
xchrisx
Beiträge: 18
Registriert: 23. August 2007 20:20
Wohnort: Duisburg
Kontaktdaten:

Beitrag von xchrisx »

solarix hat geschrieben:Da fehlt noch die Definition der statischen Variabel in der cpp-File:

So in etwa:

Code: Alles auswählen

DCMotorStateClosing* DCMotorStateClosing::pInstance = NULL;
btw: Beim Singleton-Pattern macht es keinen Sinn, "pInstance" und der Konstruktor als "public" zu deklarieren....

hth!
Hey super. Klappt. Vielen Dank.
Ja die Deklaration wird noch private gemacht. Hatte mir den Quelltext von einem UML-Programm erstellen lassen und hatte da die Zugriffsrechte noch nicht gesetzt.
Antworten