Seite 1 von 1

Problem Static Pointer

Verfasst: 22. März 2011 10:57
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 :)

Verfasst: 22. März 2011 11:17
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!

Verfasst: 22. März 2011 11:23
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.