4 Replies - 607 Views - Last Post: 13 October 2016 - 09:31 AM Rate Topic: -----

#1 ogn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-June 16

Deprecated conversion from string constant to ‘char*’

Posted 13 October 2016 - 08:47 AM

Hi guys, I a having a warning message in my code but I don't what it mean. I hope i can get some help.

Here is my code
game_creature g1("Alicorn","blue",10,"Unicorn");


warning message:
main.cpp|11|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]|
main.cpp|11|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]|
main.cpp|11|warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]|



Thank you!

This post has been edited by ogn: 13 October 2016 - 08:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Deprecated conversion from string constant to ‘char*’

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6979
  • View blog
  • Posts: 14,600
  • Joined: 16-October 07

Re: Deprecated conversion from string constant to ‘char*’

Posted 13 October 2016 - 09:13 AM

Quick test:
#include <iostream>
#include <string>

void foo(char *x) { std::cout << "foo: " << x << std::endl; }
void bar(const char *x) { std::cout << "bar: " << x << std::endl; }
void baz(const std::string &x) { std::cout << "baz: " << x << std::endl; }

int main() {
    foo("Alice");
    bar("Bob");
    baz("Chuck");

    return 0;
}



Results:
t.cc: In function ‘int main()’:
t.cc:9:16: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
     foo("Alice");
                ^



Literals are always const and your are being told to pass them that way. Make your constructor conform to one of the other two and you'll be fine.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,101
  • Joined: 05-May 12

Re: Deprecated conversion from string constant to ‘char*’

Posted 13 October 2016 - 09:17 AM

Your class constructor is likely declared like this:
game_creature::game_creature(char * name, char * color, int health, char * type);



This means that the constructor is allowed to do something like this:
strcpy(name, "Alcazar");



But obviously, if you call your constructor like you did, you'll get a segmentation fault when strcpy() tries to write into the constant string "Alicorn".

I think that the quick fix is to use const, but beware of the viral effects of using const. In general, the viral effects are a good thing, but it can be an arduous process -- much like getting the flu virus.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • Posts: 13,468
  • Joined: 08-August 08

Re: Deprecated conversion from string constant to ‘char*’

Posted 13 October 2016 - 09:28 AM

Is that a prototype??? Why use constants in the parameter list at all?
Was This Post Helpful? 0
  • +
  • -

#5 ogn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-June 16

Re: Deprecated conversion from string constant to ‘char*’

Posted 13 October 2016 - 09:31 AM

It is to call the non-default constructors

This post has been edited by ogn: 13 October 2016 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1