1

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

greenfox
  • 208
  • 1
  • 9
  • `Sound *s = new Sound();` is a bad idea already (also having those pointer members in the `Sound` class). – πάντα ῥεῖ Dec 08 '14 at 11:25
  • what is the best practice? – greenfox Dec 08 '14 at 11:29
  • _"what is the best practice?"_ Depends on what `alGenBuffers()` actually is. Are you trying to wrap a c-style API? – πάντα ῥεῖ Dec 08 '14 at 11:32
  • I'm trying to use OpenAL API. With simple variables it works perfectly.. but the problem is when I'm storing variables into an Object – greenfox Dec 08 '14 at 11:35
  • The exact error messages would provide useful clues for determining what's wrong. You should add them to the question. – molbdnilo Dec 08 '14 at 11:35
  • I just provided the error but I think that it is an error due to reading an empty buffer – greenfox Dec 08 '14 at 11:38
  • @πάνταῥεῖ however I need those pointers, I need a class to store those variables, then I will decorate class with some functions (either I would have used struct) – greenfox Dec 08 '14 at 11:40
  • @greenfox I should have said _public pointer members_. The whole point of OOP is data encapsulation, and c-style functions to initialize them can be calle from your constructor function or other public functions of your class. – πάντα ῥεῖ Dec 08 '14 at 11:47

2 Answers2

1

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);
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Just tried your solution, it runs the part of generating buffers, but later I have to call another function "playSound" which do the following: alSourcePlay(s->source); but checking with breakpoints I found that buffer and source are empty when calling that function.. :\ I have no clue – greenfox Dec 08 '14 at 15:24
  • @greenfox: I'm afraid I don't know enough about OpenAL to help you debug that. Perhaps you might ask another question, showing exactly what you're doing, and explaining what you mean by "empty". – Mike Seymour Dec 08 '14 at 15:28
  • alright I just instanced the sound before entering the function, not inside, and its OK. It clearly runs.. but there is no sound :( I think I just need pointers However thanks so much for helping me – greenfox Dec 08 '14 at 15:31
  • OK, I just made the code works. Your answer put me in right way, hence I will mark your answer as correct. Thanks! – greenfox Dec 08 '14 at 16:27
0

You are trying to access uninitialized pointers, meaning you are trying to dereference a null pointer, those pointers needs to be initialized first, for more information: What is a class constructor?

and you should also read this: Important points to Note using a constructor

In any case puting your member variables in the public section is bad practice and you should use getter and setter: Why use getters and setters?

Community
  • 1
  • 1
Ravid Goldenberg
  • 2,119
  • 4
  • 39
  • 59