static methods in c++ (makefile)
Hi,
if i wrote C/C&& Programs (on Linux Plaforms), i allways used some "just copy-paste-compile it-somehow" makefiles, i.e. i never spended much time to really understand makefiles. And exactly at this point my problem starts.
the simplifyed example of my problem is the following:
i have a class, where i want to use static methods of another class...I allways only get linker errors.(so i think i define them right) If i make it non static it all works.
Thanx for any help :-)
Can you post an example of the code that is producing the linker error?
*edit* - Nevermind. That doesn't help you.
*edit* - Nevermind. That doesn't help you.
You havn't really posted enough information, so I'll have to take a guess:
If you have put the keyword static in your .cpp file (on your function) - remove it - it only needs to be in the .h file in the class definition.
If you have put the keyword static in your .cpp file (on your function) - remove it - it only needs to be in the .h file in the class definition.
her some simplifeyed code :
//PhysikEngine.h
class PhysicEngine
{
public:
PhysicEngine();
virtual ~PhysicEngine();
static void doNothing();
.
.
.
//PhysicEngine.cpp
#include "PhysicalObject.h"
#include "PhysicEngine.h"
PhysicEngine::PhysicEngine()
{
}
PhysicEngine::~PhysicEngine()
{
}
void PhysicEngine::doNothing() {
}
.
.
.
//OpenGl class where i want to use it
void init(void)
{
PhysicEngine.doNothing();
axis.setvalue(0,0);
axis.setvalue(1,1);
axis.setvalue(2,0);
GLfloat mat_specular[] = { 1.0, 1.0,
//Makefile
RM = /bin/rm -f
C++ = g++
CCFLAGS = -I. -I/usr/X11R6/include -L/usr/X11R6/lib --verbose
LDFLAGS = -lpthread -lglut -lGLU -lGL -lm -lXi -lX11 -lXmu -lXext
clean:
@echo --- $@ ---
-$(RM) *.o core
all: PhysicEngine.h PhysicEngine.o PhysicalObject.o PointMass.o SMatrix.o Vektor.o
@echo --- $@ ---
$(C++) $(CCFLAGS) GameEngine.cc Vektor.o SMatrix.o PhysicEngine.o PhysicalObject.o PointMass.o -o GameEngine $(LDFLAGS)
Vektor.o: Vektor.cc Vektor.h
$(C++) -c $(CCFLAGS) -o Vektor.o Vektor.cc $(LDFLAGS)
SMatrix.o: SMatrix.cc SMatrix.h
$(C++) -c $(CCFLAGS) -o SMatrix.o SMatrix.cc $(LDFLAGS)
PhysicEngine.o : PhysicEngine.cpp PhysicalObject.h
$(C++) -c $(CCFLAGS) -o PhysicEngine.o PhysicEngine.cpp $(LDFLAGS)
PhysicalObject.o : PhysicalObject.cpp PointMass.h
$(C++) -c $(CCFLAGS) -o PhysicalObject.o PhysicalObject.cpp $(LDFLAGS)
PointMass.o : PointMass.cpp SMatrix.h
$(C++) -c $(CCFLAGS) -o PointMass.o PointMass.cpp $(LDFLAGS)
i removed the static in the .cpp file :
now i get the following error message :
GameEngine.cc: In function `void init()':
GameEngine.cc:22: error: parse error before `.' token
make: *** [all] Error 1
if i do it like this :
PhysicEngine e;
e.doNothing();
it works.
//PhysikEngine.h
class PhysicEngine
{
public:
PhysicEngine();
virtual ~PhysicEngine();
static void doNothing();
.
.
.
//PhysicEngine.cpp
#include "PhysicalObject.h"
#include "PhysicEngine.h"
PhysicEngine::PhysicEngine()
{
}
PhysicEngine::~PhysicEngine()
{
}
void PhysicEngine::doNothing() {
}
.
.
.
//OpenGl class where i want to use it
void init(void)
{
PhysicEngine.doNothing();
axis.setvalue(0,0);
axis.setvalue(1,1);
axis.setvalue(2,0);
GLfloat mat_specular[] = { 1.0, 1.0,
//Makefile
RM = /bin/rm -f
C++ = g++
CCFLAGS = -I. -I/usr/X11R6/include -L/usr/X11R6/lib --verbose
LDFLAGS = -lpthread -lglut -lGLU -lGL -lm -lXi -lX11 -lXmu -lXext
clean:
@echo --- $@ ---
-$(RM) *.o core
all: PhysicEngine.h PhysicEngine.o PhysicalObject.o PointMass.o SMatrix.o Vektor.o
@echo --- $@ ---
$(C++) $(CCFLAGS) GameEngine.cc Vektor.o SMatrix.o PhysicEngine.o PhysicalObject.o PointMass.o -o GameEngine $(LDFLAGS)
Vektor.o: Vektor.cc Vektor.h
$(C++) -c $(CCFLAGS) -o Vektor.o Vektor.cc $(LDFLAGS)
SMatrix.o: SMatrix.cc SMatrix.h
$(C++) -c $(CCFLAGS) -o SMatrix.o SMatrix.cc $(LDFLAGS)
PhysicEngine.o : PhysicEngine.cpp PhysicalObject.h
$(C++) -c $(CCFLAGS) -o PhysicEngine.o PhysicEngine.cpp $(LDFLAGS)
PhysicalObject.o : PhysicalObject.cpp PointMass.h
$(C++) -c $(CCFLAGS) -o PhysicalObject.o PhysicalObject.cpp $(LDFLAGS)
PointMass.o : PointMass.cpp SMatrix.h
$(C++) -c $(CCFLAGS) -o PointMass.o PointMass.cpp $(LDFLAGS)
i removed the static in the .cpp file :
now i get the following error message :
GameEngine.cc: In function `void init()':
GameEngine.cc:22: error: parse error before `.' token
make: *** [all] Error 1
if i do it like this :
PhysicEngine e;
e.doNothing();
it works.
To call a static member function you should use two colons instead of one dot:
Greetz,
Illco
PhysicEngine::doNothing();
Greetz,
Illco
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement