Search code examples
c++functionvoidirrklang

irrklang engine was not declared at void


EDIT 2: Solved! Use the code below and it worked!

irrklang::ISoundEngine* engine = irrklang::createIrrKlangDevice();

Just place the code above at the top of the code. (Maybe the next line of include or namespace)


I'm using irrKlang to play audio but I had a problem:

#include <irrKlang.h>

void playSound() {
    engine->play2D("src/Click.wav");
}

int main() {
    irrklang::ISoundEngine* engine = irrklang::createIrrKlangDevice();
    playSound();

    engine->drop();
    return 0;
}

When I run this code, it show that 'engine' (that in the void) was not declared in this scope.

I test this at int main but it work. The problem is that it only worked at main but not at void.

Anything I can use to fix this error? Or is it a bug?

Thanks in advance.


Solution

  • That is expected. irrklang::ISoundEngine* engine is defined in main function but not in playSound().

    A straightforward solution would be to pass engine as an argument

    void playSound(irrklang::ISoundEngine* engine) {
        engine->play2D("src/Click.wav");
    }
    

    and in main call it like this

    playSound(engine);