简体   繁体   中英

invalid argument type with const char* and char*

I have a working example from another project that is identical to this, and it works. But for some reason in this project I cannot do it and I'm having one hell of a time debugging it.

I have a parent class with a constructor:

Shape (char* _name, char*_colour);

I then proceed to create another child class that uses Shape's constructor:

Square::Square(char* _colour, float _sideLength) : Shape("Square", _colour) 

I get a error that const char* does not work, specifically with

:Shape("Square",_colour)

But I litterally have the exact same thing with a different project doing the exact same kind of casting and it works with no bugs. I'm currently blown away.

The working example ..

Car(char* whichType, int gasConsumption);

and a child class

Minivan::Minivan(char* whoMade, int mpg, int seating, int space, char* whatColor) : Car("Minivan",mpg)

which works fine. Any idea whats going on?

An explanation might be that one project uses C++ standard older than C++11, and the other uses C++11 or later standard.

Implicit conversion from string literal to char* is ill-formed since C++11, so the compiler is not required to accept such conversion. Prior to C++11 the implicit conversion was deprecated, but well-formed and thus compilers were required to accept the program.

The fix is to use const char* instead.

Try the following -

char _name           = "YOUR_NAME";      // this is changed

const char* colour  = "YOUR_COLOUR";  // this is fine

Shape (char* _name, const char*_colour);

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