简体   繁体   中英

Cannot convert parameter 1 from derived pointer to base class pointer reference

i have class that inherits from base class , then i have method that excepts as parameter pointer reference to base class . when i try to pass the inherited object to the method im getting :

c++ cannot convert parameter 1 from 'ReelSymbol *' to ' Sprite *&'

Sprite is frame work base class

class ReelSymbol :public Sprite 
{
    public:
          ReelSymbol();
          ~ReelSymbol();

};

this is the method that i try to pass the ReelSymbol object pointer to

void UT::setSpriteReelsSymbolBoundingBoxSize(Sprite* &sprite)
{
    //DOTO to think if to use scale or bounding box size
    sprite->setScale(pSettings->ReelSymbolScaleFactor);
}

like this :

ReelSymbol* sprite = new ReelSymbol();
ut.setSpriteReelsSymbolBoundingBoxSize(sprite);

and getting this error:

c++ cannot convert parameter 1 from 'ReelSymbol *' to ' Sprite *&'

The problem with what you're trying to do is that if it would compile, then the function setSpriteReelsSymbolBoundingBoxSize could reset the variable you pass to something that is a Sprite* , but not a ReelSymbol* .

To clarify, consider the following (and pretend it would compile):

class NotAReelSymbol : public Sprite
{
};

void UT::setSpriteReelsSymbolBoundingBoxSize(Sprite* &sprite)
{
    sprite = new NotAReelSymbol;
}

ReelSymbol* sprite = new ReelSymbol();
ut.setSpriteReelsSymbolBoundingBoxSize(sprite);

Now, what's the original sprite variable of type ReelSymbol* supposed to look and behave like? This should demonstrate why the code you were trying to compile is just not valid and the compiler correctly rejects to compile it.

A possible workaround might be:

ReelSymbol* sprite = new ReelSymbol();
Sprite* sprite_base = sprite;
ut.setSpriteReelsSymbolBoundingBoxSize(sprite_base);

If you could guarantee that setSpriteReelsSymbolBoundingBoxSize will not reset the original variable to something that is not a ReelSymbol* , then afterwards you could do

sprite = dynamic_cast<ReelSymbol*>(sprite_base);

However, I find it far more likely, that you don't need to reset the original variable. Instead, by declaring the parameter as reference you were trying to express that the function is allowed to change the object to which spirit points. But since it's a pointer variable it's already clear, that the function can modify it. Thus, simply remove the & -symbol to make your code compile and work as expected:

void UT::setSpriteReelsSymbolBoundingBoxSize(Sprite* sprite);

The only problem here is your pointer reference

Sprite* &sprite

There is no need to make it a reference because you already have the pointer. Like this:

Sprite* sprite

Your compiler would automatically down cast it to your base class "Sprite" as it was expected by you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM