5 Replies - 3244 Views - Last Post: 17 May 2011 - 07:53 AM Rate Topic: -----

#1 level321  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-May 11

Error in warning: deprecated conversion from string constant to 'c

Posted 16 May 2011 - 06:58 PM

I am a beginner programming and can't figure out this error. Thanks

//Package Shipping Project
#include <iostream>
#include<string>
#include<iomanip>
using namespace std;

class Package// declare base class


{
private:
public:
   Package(char *, char *);  //constructor
  ~Package(); //destructor
   virtual float calcShipcost()=0;  //zero to make abstract
   virtual void print()=0;
protected:   

char *first, *last;  //for first and last name

   int Numofitems;
   int overnightd;
   int insurance;
   
};


Package :: Package(char *f, char *l)
{
first=new char[strlen(f)+1];
strcpy(first, f);
last= new char [strlen(l)+1];
strcpy(last, l);
}

Package::~Package() // using destructor
{
  delete[] first;
  delete[] last;
}

void Package:: print()
{ 
   cout<< first<< "  " << last;
}





class Tshirts : public Package // declaring derived class of tshirts with base class of Package

{




public:
   Tshirts(char *,char *, int, int, int);// constructor
  ~Tshirts(); //destructor
  virtual float calcShipcost();
  virtual void print()=0;

};


float Tshirts:: calcShipcost()
{

if(overnightd == 1 && insurance == 1 )//zero no and one is yes

      return (Numofitems*2.75)+10+(.02*(Numofitems*2.75));//for yes on insuranances and yes delivery
else  if      (overnightd==1 && insurance==0)
   return (Numofitems*2.75)+10;
 else  if    (overnightd==0  && insurance==1)
     return(Numofitems*2.75)+(.02*(Numofitems*2.75)) ;
else   if
    (overnightd==0 && insurance==0)
     return(Numofitems*2.75) ;
      
}

void Tshirts:: print()
{
cout<<"T shirts cost is:";
Package::print();
}



int main()
{

[b]Tshirts tees("Joe", "Smith", 2,1,0);[/b]
tees.print();//T shirts cost is:$15.50
cout<<"$"<<tees.calcShipcost()<<endl;


system("PAUSE");// must be before return 0
return o;
}





cc1plus: warnings being treated as errors
In function 'int main()':
Line 95: warning: deprecated conversion from string constant to 'char*''
Line 95: warning: deprecated conversion from string constant to 'char*''
Line 95: error: cannot declare variable 'tees' to be of abstract type 'Tshirts'
compilation terminated due to -Wfatal-errors.




Line 95 is bold

This post has been edited by level321: 16 May 2011 - 06:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Error in warning: deprecated conversion from string constant to 'c

#2 Seta00  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 84
  • Joined: 22-September 10

Re: Error in warning: deprecated conversion from string constant to 'c

Posted 16 May 2011 - 07:19 PM

GCC 4.something introduced this. Use -Wno-write-strings to get rid of them.
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2014
  • View blog
  • Posts: 5,395
  • Joined: 27-December 05

Re: Error in warning: deprecated conversion from string constant to 'c

Posted 16 May 2011 - 07:20 PM

It's not serious. It's occurring because your Tshirts constructor calls for arguments of type char* and you are supplying two string literals. Implicitly, when the function asks for char* arguments, the arguments are expected to be variables that can be modified by the function. But string literals can't be modified so strictly speaking they are inappropriate as arguments to the function as you have written its signature.

Not a problem, because your constructor doesn't, and shouldn't, try to modify the strings that are passed to it as arguments. Therefore, just change the constructor's signature to take arguments of type const char * (thus "promising" that the constructor will not try to modify the arguments) and the compiler will stop complaining about the string literals.

e.g., new declaration:
Tshirts(const char *,const char *, int, int, int);// constructor

and also change the implementation accordingly.
Was This Post Helpful? 1
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: Error in warning: deprecated conversion from string constant to 'c

Posted 17 May 2011 - 03:06 AM

It's in the FAQs topic pined at the top of the forum.
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2014
  • View blog
  • Posts: 5,395
  • Joined: 27-December 05

Re: Error in warning: deprecated conversion from string constant to 'c

Posted 17 May 2011 - 06:54 AM

View PostJackOfAllTrades, on 17 May 2011 - 05:06 AM, said:

It's in the FAQs topic pined at the top of the forum.

I suggest changing that FAQ item. It's just subverting a warning message that was put into the compiler for a valid reason.

Casting the string literal to char* will make the warning message "go away" in this instance, but if you do that with a function (or constructor) that actually tries to modify the string passed as its argument, it will compile without a warning and then crash when you run it.

For example:
#include <iostream>
using namespace std;

void changer( char * str ) {
    str[0] = 'J';
    cout << str << endl;
}

int main() {
    changer( const_cast<char*>("hello") );
    return 0;
}


Wouldn't it be better for a function that doesn't modify it's argument to say so by declaring the argument const?
e.g.:
#include <iostream>
using namespace std;

void non_changer( const char * str ) {
    cout << str << endl;
}

int main() {
    non_changer( "hello" );
    return 0;
}


This post has been edited by r.stiltskin: 17 May 2011 - 06:55 AM

Was This Post Helpful? 3
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: Error in warning: deprecated conversion from string constant to 'c

Posted 17 May 2011 - 07:53 AM

An absolutely valid point! I will remove that FAQ. My bad.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1