null was not declared in this scope?

I get "null was not declared in this scope" with the following code:

  if (wave != null) { wave.stop(); } // Halt playback of sound currently playing.

wave is an object, and I expected that if it hadn't been created yet, that the pointer would be null, but the code doesn't work. Do I just set it to 0?

I also would like to know if it is safe to set the pointer to null if I no longer need the object, or if I need to delete it somehow. Is there a garbage collector that takes care of it? I noticed the waveHC example code just seems to overwrite the pointer whenever a new file is loaded, so I would assume that setting it to null would have the same effect. But I'm fairly new to c++, so I don't know for sure if it has garbage collection system like another object oriented language I used to use did.

wave is an object, and I expected that if it hadn't been created yet, that the pointer would be null

Wrong language. C# uses null. C++ uses NULL. Also, it isn't, unlike in that idiot language, necessary to test for NULL.

if(ptr)

is how to test for a NULL pointer.

Well if C++uses "NULL" then why is "null" highlighted in the IDE?

And when you say it's not necessary to test for NULL, do you mean I don't need to test for NULL, as in I can just write wave.stop() and if wave is NULL it won't do anything?

Or do you just mean I can treat it like a boolean, and do if (ptr) to see if an object exists, or if (!ptr) to see if it does not?

do you mean I don't need to test for NULL, as in I can just write wave.stop() and if wave is NULL it won't do anything?

No. I mean that testing for == NULL is like testing for == true or == false. The test is implicit in the value being compared.

Or do you just mean I can treat it like a boolean, and do if (ptr) to see if an object exists, or if (!ptr) to see if it does not?

Exactly.

Okay, I figured I could do that, I just preferred to write it out explicitly.

I'm a little concerned about what I've been reading about NULL and C++ though:

For example here is says you can't write objectname = NULL. But I don't understand why you can't do that.

When I declare "WaveHC wave", one would assume wave contains null. Then I create a WaveHC object to go in it. Then maybe I replace that WaveHC object in it with another when I go to play a new sound. And that should work. That seems to be what the sample code does.

But then say I want to get rid of the WaveHC object in wave? I can't set wave to NULL to do that? Why not? From what I'm reading it sounds like they're saying there's NO way to get rid of the object and that the object pointer should never be able to contain NULL. That doesn't make sense.

For example here is says you can't write objectname = NULL. But I don't understand why you can't do that.

Because the object exists. You can delete an object, explicitly using delete or implicitly by letting it go out of scope. Objects either have been created, or they haven't. Pointers to objects can have been pointed to something, or not. If not, they have a value of NULL.

When I declare "WaveHC wave", one would assume wave contains null.

No, because you are declaring an instance of the WaveHC class, which invokes the constructor. You are not defining a pointer to n instance of a class that will (maybe) be valued later.

But then say I want to get rid of the WaveHC object in wave?

Why? You could declare/create the instance in a smaller scope. The destructor will be called when the object goes out of scope.

I can't set wave to NULL to do that?

No.

From what I'm reading it sounds like they're saying there's NO way to get rid of the object and that the object pointer should never be able to contain NULL. That doesn't make sense.

There is no way to get rid of a static object. If you create a dynamic instance of the class, using pointers and new, you can get rid of the object.

WaveHC *pWave = new WaveHC();
if(pWave)
{
   pWave->doSomething();

   delete pWave;
   pWave = NULL;
}

Sorry, I think I was getting confused about what wave.create() was doing in the code. It's not creating a wave object, it's just initializing one that already exists.

I guess that means I don't even need those NULL checks in there. Because even if I haven't loaded a sound file to play, that wave object is still going to be there, and I can check it to see if it is currently playing or not.

scswift:
wave is an object, and I expected that if it hadn't been created yet, that the pointer would be null, but the code doesn't work. Do I just set it to 0?

You seem to be treating wave as a pointer and an object simultaneously. Sure it can only be one or the other?

Yes, I was just mixed up. I came from BlitzMax to C++ and in Blitzmax you store objects in pointers. So "mysprite = new sprite" would be how you would create an object. In the audio lib I'm using the person that wrote it chose to call the function that initializes the wave data wave.create(), which made me think it was creating a new object each time I called it. But in reality, there's just one global WaveHC object called wave, and wave.create() just loads it with new data.

I guess the idea is you're supposed to be able to create multiple WaveHC objects for different sounds and play, pause, resume, and stop them separately, but since one WaveHC object has no way to know the others exist, it doesn't make a lot of sense, because sound1.isplaying would return true even when I've called sound2.play(), which would cause that sound to play instead.

Anyway I think I've got it fixed now, everything compiled, I just need to test it.