Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illegal call to non-static member function (C++)?

Tags:

c++

scope

I'm developing a game which is based around the user controlling a ball which moves between areas on the screen. The 'map' for the screen is defined in the file ThreeDCubeGame.cpp:

char m_acMapData[MAP_WIDTH][MAP_HEIGHT];

The ThreeDCubeGame.cpp handles most of the stuff to do with the map, but the player (and keyboard input) is controlled by ThreeDCubePlayer.cpp. When a player moves into a new map cell, the game will have to check the contents of that cell and act accordingly. This function in ThreeDCubeGame.cpp is what I am trying to use:

inline char GetMapEntry( int iMapX, int iMapY ) { return m_acMapData[iMapX][iMapY]; }

So, in order to check whether the player is allowed to move into a map cell I use this function call from ThreeDCubePlayer.cpp:

if (ThreeDCubeGame::GetMapEntry(m_iMapX+MAP_OFF_X, m_iMapY+MAP_OFF_Y) == ' ')
{
// do stuff
}

But, when I compile this, I get the warning "error C2352: 'ThreeDCubeGame::GetMapEntry' : illegal call of non-static member function". Is this something to do with the scope of the variables? Is it fixable without redesigning all the code?

like image 802
benwad Avatar asked Dec 04 '09 13:12

benwad


People also ask

What is non static member function in C?

final (C++11) [edit] A non-static member function is a function that is declared in a member specification of a class without a static or friend specifier. ( see static member functions and friend declaration for the effect of those keywords)

Can a static function call a non static function in C?

A static method provides NO reference to an instance of its class (it is a class method) hence, no, you cannot call a non-static method inside a static one.

What happens if non static members?

What happens if non static members are used in static member function? Explanation: There must be specific memory space allocated for the data members before the static member functions uses them. But the space is not reserved if object is not declared.

Can a static member function call non static member function of a class?

You cannot have static and nonstatic member functions with the same names and the same number and type of arguments. Like static data members, you may access a static member function f() of a class A without using an object of class A .


2 Answers

class A {
  int i;
public:
  A(): i(0) {}
  int get() const { return i; }
};

int main() {
  A a;
  a.get();  // works
  A::get(); // error C2352
}

There's no object to call the function with.

like image 183
Didier Trosset Avatar answered Oct 02 '22 00:10

Didier Trosset


GetMapEntry is not static so you can't call it without an object of the type ThreeDCubeGame.

Alternatives:
-Make GetMapEntry static: static inline char GetMapEntry
-Create an instance of ThreeDCubeGame and do instance.GetMapEntry(

like image 29
Arkaitz Jimenez Avatar answered Oct 02 '22 00:10

Arkaitz Jimenez