Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 13-Sep-12 5:32am
Comments
pwasser at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

A static function inside a class is basically the same as a function outside the class:
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:
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;
}
  Permalink  
v4
Comments
Pravinda Amarathunge at 22-Sep-12 1:17am
   
Thanks, I read more from google and i got it. Thanks though!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
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.
  Permalink  
Comments
Pravinda Amarathunge at 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)



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 13 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100