Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
C++
class myClass
{
public:
   static int sFunc();
   int iAdd(int a, int b);
};

int myClass::sFunc()
{
   return iAdd(124,142); // ERROR apears here ; a nonstatic member reference must be relative to a specific object
}



Why is that and how to prevent the error ?
Posted
Comments
[no name] 13-Sep-12 19:53pm    
Two points.

1. This is a very basic question. Had you Googled the error text message you would have got all the answers posted here.
2. If you are satisfied with the answers, they are correct, you should post some votes.

A static function inside a class is basically the same as a function outside the class:
C++
class myClass
{
public:
   int iAdd(int a, int b);
};

int sFunc()
{
   return iAdd(124,142);
}

Pretty weird, isn't it?

The only differences between a static method and a function outside the class is that a static method can see the private members of the class while the outer function doesn't, and code outside the class can access the outer function while the static method might be put to protected or private hiding it from most of the outer world.

Inside a static method you can reach even private members of the specified class but you have to pass somehow an instance of that class to that function first (because static methods are like normal functions, without a this pointer to a class instance). For example if you instantiate an instance of myClass as a global variable then your static method can access it, or you can simulate the this pointer by passing in a myClass instance as a parameter to your static method, or you can just create an instance for yourself inside the static method like the meyer's singleton pattern:
C++
class myClass
{
public:
    // meyers singleton, works fine
    static myClass& GetInstance()
    {
        static myClass g_Instance;
        return g_Intance;
    }
private:
    myClass() {}
};

// This wouldn't work because this GetInstance() doesn't see the private constructor of the class.
myClass& GetInstance()
{
    static myClass g_Instance;
    return g_Intance;
}
 
Share this answer
 
v4
Comments
Captain Price 22-Sep-12 1:17am    
Thanks, I read more from google and i got it. Thanks though!
the method iAdd must be static too.
You cannot call a non static method from a static method.
I suggest you read a C++ book to read a little bit on what the difference is between a static method and an instance method. While you are at it, you will have to learn what an instance is.

myClass a;

-> a is an instance of myClass
-> a.iAdd(5,5); would work, but if it returns 10, then there is no point in making it a normal member because it does not use anything of the myClass class. so if iAdd(int a, int b) is return a+b; , then it is correct to make it static.

But you have some reading to do this is all pretty basic stuff.
 
Share this answer
 
v2
sFunc is static, so is not related to any particular instance of the class.
iAdd is not static, so it requires an instance of the class in order to be called.

Consider the following code:
C++
myClass::sFunc();


This would normaly be valid, but think, how is sFunc() going to call iAdd() without an instance of the class? Answer is; it can't. And that is why you get the error.

Hope that helps.
 
Share this answer
 
Comments
Captain Price 24-Sep-12 8:20am    
Thanks though !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900