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

I am developing a game based on a user controlling a ball that moves between areas on the screen. The "map" for the screen is defined in the ThreeDCubeGame.cpp file:

char m_acMapData[MAP_WIDTH][MAP_HEIGHT]; 

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

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

So, to check if the player is allowed to move to the 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 a non-static member function". Is this something related to the volume of variables? Is it fixable without changing the whole code?

+4
source share
7 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 is no object to call function c.

+9
source

GetMapEntry is not static , so you cannot call it without an object of type ThreeDCubeGame.

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

+4
source

ThreeDCubeGame is a class, not an instance, so you can only use it to access static members (that is, a member function with the static ) You must create an instance of the object of this class to use non-static elements

 ThreeDCubeGame map; ... map.GetMapEntry(iMapX, iMapY). 
+1
source

You are trying to call a class method. Is that what you intend? Or do you want GetMapEntry be an instance method? If it is a class method, it should be marked as static. If this is an instance method, you need to call it with an instance of ThreeDCubeGame . Also, is GetMapEntry even a member of the class?

0
source

The error indicates that you call the GetMapEntry function as static, while you declare it as a member function. You need:

  • call it through an instance of ThreeDCubeGame: threedcubegameinstance.GetMapEntry() ,
  • declares the GetMapEntry function static (add static before the built-in and make m_acMapData static too).
0
source

You are missing the static keyword.

 // .h class Playfield { public: static char GetTile( int x, int y ); // static on a method means no 'this' is involved }; // .cpp static char tiles[10][10] = {}; // static on vars in .cpp prevents access from outside this .cpp char Playfield::GetTile( int x, int y ) { // handle invalid args // return tile return tiles[x][y]; } 

Other options if you want only one unique playing field: You can make Playfield single, turn it into a namespace or use global functions. The result will be the same from the perspective of the caller.

On the side of the note: Since they all use a static and / or global variable, they are inherently not thread safe.

If you need several playing fields and / or want to play in safe mode using multithreading and / or want to do it in OOP mode, you will need a Playfield instance to call the function (pointer 'this'):

 class Playfield { public: char GetTile( int x, int y ) const { return this->tiles[x][y]; } // you can omit 'this->', but it inherently present because // the method is not marked as static public: Playfield() { /*you will have to initialize 'this->tiles' here because you cannot use the struct initializer '= {}' on member vars*/ } private: char tiles[10][10]; }; 

The calling code will use Playfield as follows:

 void main() { // static version char tile11 = Playfield::GetTile( 1, 1 ); // non-static version Playfield myPlayfield; char tile12 = myPlayfield.GetTile( 1, 2 ); } 
0
source

It may be useful to have a class containing a set of functions, without any data members, if you do not want to expand auxiliary functions.
Otherwise, it would be more practical to use a namespace to collect these functions.
Example:

 class Solvers { public: void solve_a(std::vector<int> data); void solve_b(std::vector<int> data, int value); private: int helper_a(int a, int b); } 

But the class must be initialized before use.
The easiest way to make these functions useful is to mark them static in the class:
static void solve_a(std::vector<int> data);
Then member functions can be used as:
Solver::solve_a(my_vector);

Another way would be to initialize the class before using:
Solver solver;
solver.solve_a(my_vector);

And the third method, not mentioned earlier, by default initializes it during use:
Solver().solve_a(my_vector);

0
source

All Articles