Compilation Error

Started by
6 comments, last by Reckoner 17 years, 2 months ago
Hi, I have two methods like so:

using namespace test;

void AudioManager::ScheduleLoad( Sound *pSound );

void Sound::Load()
{

    // snip

    g_pAudioManager->ScheduleLoad( this );

    // snip

}
This code gives this error at compile time: error C2664: 'void test::AudioManager::ScheduleLoad(Sound *)' : cannot convert parameter 1 from 'test::Sound *const ' to 'Sound *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Does anyone know why this doesn't compile? I kknow that this is const, but changing the Scheduling method to void AudioManager::ScheduleLoad( const Sound *pSound ); just causes an error which says that Sound *const cannot be converted to const Sound * Any help would be very much appreciated, thanks.
Advertisement
Your this pointer is apprently of type Sound * const, which would happen if your Load method is a const method (in short, you guaranteed that the method would not change the object pointed by this).

Are you sure the method is non-const?
100% sure. The declaration loks like this:
class AudioManager : public IAudioManager{public:    void ScheduleLoad( Sound *pSound );};class Sound : public ISound{public:    bool Load();};


Obviously I have removed all of the other method, if it makes a difference.

Thanks.
Quote:Original post by jpbb
cannot convert parameter 1 from 'test::Sound *const ' to 'Sound *'


By chance, do you happen to have a class Sound outside of namespace test? The error message seems to be saying that you are trying to pass a test::Sound* while the function wants a ::Sound*.

Quote:Original post by ToohrVyk
Your this pointer is apprently of type Sound * const


Sound * const would mean a constant pointer to Sound, not a pointer to a const Sound. this acts like a constant pointer in that it can not be assigned to, so that is probably the reason why the error message gives the type as Sound* const. Had Load been const, the type of this would have been const Sound*.
Quote:Original post by Reckoner
Sound * const would mean a constant pointer to Sound, not a pointer to a const Sound. this acts like a constant pointer in that it can not be assigned to, so that is probably the reason why the error message gives the type as Sound* const. Had Load been const, the type of this would have been const Sound*.


I guess this confirms once again how much I hate C types. You are, of course, completely right.
Quote:Original post by Reckoner
Sound * const would mean a constant pointer to Sound, not a pointer to a const Sound. this acts like a constant pointer in that it can not be assigned to, so that is probably the reason why the error message gives the type as Sound* const. Had Load been const, the type of this would have been const Sound* const.


Fixed, if I'm thinking clearly. :) The this-pointer itself is const whether or not the pointed-at thing is const.

(Of course, it *should* be a reference, anyway, but that's the language we live with :( )
Fixed.

I was using a few forward class declarations, and hadn't put them inside the namespace.
Quote:Original post by Zahlman
Fixed, if I'm thinking clearly. :) The this-pointer itself is const whether or not the pointed-at thing is const.


Sorry, I didn't express myself clearly. The standard says (9.3.2.1) that the type of this in a non-const member function of X is X*, and in a const member function const X*. But it is also not an lvalue, so it acts like an X* const in most (but not all) cases, and appears to be reported as such by Visual C++.

[Edited by - Reckoner on January 29, 2007 8:41:52 PM]

This topic is closed to new replies.

Advertisement