Search code examples
c++objectopenal

accessing public object property for OpenAL buffer


I created an object with two public properties:

class Sound {
public:
    unsigned int * source; 
    unsigned int * buffer;
}

Now I want to access to these variables but I'm stucked.. the compiler compiles without errors, but when I do something like

Sound *s = new Sound();
alGenBuffers(1, s->buffer);
alGenSources(1, s->source);

It throws errors.. passing in those functions a simple unsigned int * variable it works, but I want an object oriented style.

What I'm doing wrong? I even tried with getter and setter, but same error is thrown..

Thanks

EDIT: sorry I just did a typo while copying my code here, it was s->buffer and s->source.

Error thrown: Not handled exception in 0x00894E93 in project.exe 0xC0000005; Violation access while reading 0x00000000


Solution

  • You're passing a null pointer to each function. Instead, you want a pointer to an unsigned int variable which will be used to store the result. (In general, they want a pointer to an array; but in this case you're just creating one of each, so a single variable for each will suffice.)

    class Sound {
    public:
        unsigned int source; 
        unsigned int buffer;
    };
    
    alGenBuffers(1, &s->buffer);
    alGenSources(1, &s->source);