Passing a string literal to a constructor??

I have a class called Product. It has three data members I need to have several different constructors. On of them will take three arguments for the data members. One of these data members is a char*. I have set up its setter so that it dynamically allocates the memory for it. It works just fine as long as i set char[] and set it equal to the string and then equal to a pointer and pass the pointer to the setter. But when i try and pass the string strait to the constructor it tells me "Conversion from string literal to 'char' is deprecated".
The program runs fine and does what it is suppose to, but i have that warning i would like to get rid off.

Here's my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Product myproduct(123456, "Products description here", 5.87); // creates object with these arguments

Product (long, char[], double); // user constructor

Product::Product (long productNum, char productDesc[], double productCost)// implementation of constructor
{
     char* description = productDesc;

     setProductNumber(productNum);
     setProductDescription(description);
     setUnitCost(productCost);
} 

void Product::seProductDescription(char* description)//setter for the description
{
sizeOfString = int(strlen(description));
productDescription = new char [sizeOfString +1];
strcpy(producDescription, description);
}


I'm doing this in XCode don't know if that makes a difference. Also my destructor does something weird. When the program terminates i get a message saying "malloc: *** error for the object 0x100100a20: pointer being freed was not allocated" The error at the implementation is in green and says "Thread1: Program received signal: "SIGABRT"." I'm not sure what this means.
Last edited on
Your intent appears to be string manipulation. Use strings, then, not arrays of char.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Product (long, const string&, double); // user constructor

Product::Product (long productNum, const string& productDesc, double productCost)
    : productDescription(productDesc),
      productNumber(productNum),
      unitCost(productCost) // just guesing.
{
} 

// consider removing this, or at least rename to "changeDescription".
void Product::seProductDescription(const string& description)
{
    productDescription = description;
}


As for the diagnostics, conversion from a string literal to pointer to char was deprecated but permitted in older revisions of the C++ language, to ease incorporation of C source code. It is not valid C++ according to the current language rules.

And your destructor apparently attempts to delete[] (or delete) a pointer that was not obtained from new[] (or new).
Last edited on
Instead of taking in a char*, setProductDescription should take a const char*. Also, the constructor should take a const char* as opposed to a char[]. (or use strings as Cubbi suggested)

malloc: *** error ... pointer being freed was not allocated

You've allocated your char* with new[]. Hence, you should be using delete[] (not free) to deallocate the memory.
Last edited on
I would love to use strings but unfortunately I'm not allowed to:) I will try the conts char*. I guess i just replace my char[] In the two spots and i'll be good. As for the destructor I believed i used what you'r talking about delete [] productDiscription; , right?
shacktar the const char* worked perfectly. Thanks. Man points and the such sure are confusing.
Topic archived. No new replies allowed.