简体   繁体   中英

Why does this code generate the compiler error C2227?

I'm working on integrating my current game engine with the irrKlang sound engine, and am dealing with a persistent error. Simplified:

fsCore.h

class fsEngine
{
public:
    static fsEngine *getInstance();
    static void release();
    ;
private:
    static fsEngine *instance;
    static fsBool exists;
    irrklang::ISoundEngine *soundEngine;
};

fsCore.cpp

#include "fsCore.h"
void fsEngine::release()
{
    exists = false;
    delete instance;
    soundEngine->drop(); //C2227
};

The engine is being declared correctly, and the singleton is performing as expected. Any ideas?

Explanation of C2227 can be found here: Compiler Error C2227 .

When the compiler gets to this line:

soundEngine->drop(); //C2227

it tells you that soundEngine must be pointer to class / struct / union in order to call drop() on it. The actual problem here is that you're trying to access the non-static data member from static function.

Also note that delete doesn't changes the value of pointer itself, so after this line is executed:

delete instance;

the value of instance is still set to the same address, this pointer has became invalid ( dangling ). It is a good practice to assign NULL to the pointer after you delete it:

delete instance;
instance = NULL;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM