Robolisk
Robolisk

Reputation: 1792

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?

Upvotes: 0

Views: 273

Answers (2)

Dany
Dany

Reputation: 23

Try the following -

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

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

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

Upvotes: 0

eerorika
eerorika

Reputation: 238461

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.

Upvotes: 1

Related Questions