Static member function

May 8, 2008 at 4:04pm
Hi, I have a problem declaring a method of a class.Suppose that I have class "X", and within class "X" I have a method that returns an instance of "X". For example, in the .h file of "X" I have the method declared as the following:

X Method(int);

And in the .cpp file I have implemented the method in this way:

1
2
3
X X::Method(int number){

}


When I compiled, I received this errors:

X X::Method is not a static member of class X
X was not declared in this scope
expected ',' or ';' before '{' token

Can someone tell me what is happening here?

Regards.
May 8, 2008 at 4:30pm
You simply need to add the static keyword in the .h file:

 
static X Method(int);


Static methods are methods that belong to the class, instead of individual object (instances) of the class. The compiler gave you an error, because you tried to call an instance method without an object as receiver.
May 8, 2008 at 7:05pm
Thank you! I solved the problem

Best regards
Topic archived. No new replies allowed.