problem with dynamic constructors, (cannot convert string literal to (char *) ).
-
#include
#includeusing namespace std;
class String{
char *name;
int length;public:
String(){ //default constructor
length = 0;
name = new char[length + 1];
}
String(char *s) //parameterised constructor
{
length = strlen(s);
name = new char[length + 1] ;strcpy(name, s); } void display(void){ cout<
i should get output :
joseph
louis
lagrange
Joseph Louis
Joseph louis lagrangebut instead i am getting:
Joseph
louis
lagrange
louis
lagrangename = new char\[length + 1\]; //dyanamic allocation strcpy(name, a.name); strcpy(name, b.name);
You are copying both names to the same place instead of concatenating them using
strcat
. A better way would be something like:name = new char\[length + 2\]; //dyanamic allocation strcpy(name, a.name); // copy the first name strcat(name, " "); // append a space character after the first name strcat(name, b.name); // append the second name
-
#include
#includeusing namespace std;
class String{
char *name;
int length;public:
String(){ //default constructor
length = 0;
name = new char[length + 1];
}
String(char *s) //parameterised constructor
{
length = strlen(s);
name = new char[length + 1] ;strcpy(name, s); } void display(void){ cout<
i should get output :
joseph
louis
lagrange
Joseph Louis
Joseph louis lagrangebut instead i am getting:
Joseph
louis
lagrange
louis
lagrangeYour join function is not joining but effectively setting only the second string:
void String :: join(String &a, String &b){
length = a.length + b.length ;
delete name;
name = new char[length + 1]; //dyanamic allocationstrcpy(name, a.name); // This will overwrite name with b.name //strcpy(name, b.name); // You have to append b.name instead using strcat() strcat(name, b.name); // or by copying to the current end //strcpy(name + a.length, b.name);
};
cannot convert string literal to (char *)
To avoid that message change the constructor to accept a
const char*
argument:String(const char *s)
{
length = strlen(s);
name = new char[length + 1] ;
strcpy(name, s);
} -
#include
#includeusing namespace std;
class String{
char *name;
int length;public:
String(){ //default constructor
length = 0;
name = new char[length + 1];
}
String(char *s) //parameterised constructor
{
length = strlen(s);
name = new char[length + 1] ;strcpy(name, s); } void display(void){ cout<
i should get output :
joseph
louis
lagrange
Joseph Louis
Joseph louis lagrangebut instead i am getting:
Joseph
louis
lagrange
louis
lagrange -
Your join function is not joining but effectively setting only the second string:
void String :: join(String &a, String &b){
length = a.length + b.length ;
delete name;
name = new char[length + 1]; //dyanamic allocationstrcpy(name, a.name); // This will overwrite name with b.name //strcpy(name, b.name); // You have to append b.name instead using strcat() strcat(name, b.name); // or by copying to the current end //strcpy(name + a.length, b.name);
};
cannot convert string literal to (char *)
To avoid that message change the constructor to accept a
const char*
argument:String(const char *s)
{
length = strlen(s);
name = new char[length + 1] ;
strcpy(name, s);
}Thank you sir.. here's the working code
#include
#includeusing namespace std;
class String{
char *name;
int length;public:
String(){ //default constructor
length = 0;
name = new char[length + 1];
}
String(const char *s) //parameterised constructor
{
length = strlen(s);
name = new char[length + 1] ;strcpy(name, s); } void display(void){ cout<
-
You know
C++
standard library provides thestring
class, featuring the concatenation operator[^], don't you? -
name = new char\[length + 1\]; //dyanamic allocation strcpy(name, a.name); strcpy(name, b.name);
You are copying both names to the same place instead of concatenating them using
strcat
. A better way would be something like:name = new char\[length + 2\]; //dyanamic allocation strcpy(name, a.name); // copy the first name strcat(name, " "); // append a space character after the first name strcat(name, b.name); // append the second name
-
Your join function is not joining but effectively setting only the second string:
void String :: join(String &a, String &b){
length = a.length + b.length ;
delete name;
name = new char[length + 1]; //dyanamic allocationstrcpy(name, a.name); // This will overwrite name with b.name //strcpy(name, b.name); // You have to append b.name instead using strcat() strcat(name, b.name); // or by copying to the current end //strcpy(name + a.length, b.name);
};
cannot convert string literal to (char *)
To avoid that message change the constructor to accept a
const char*
argument:String(const char *s)
{
length = strlen(s);
name = new char[length + 1] ;
strcpy(name, s);
}Quote:
To avoid that message change the constructor to accept a const char* argument:
It worked as you told, but how and why it was giving error when i was simply using char, and how i will know in which conditions to use const char ? please can you elaborate
-
Quote:
To avoid that message change the constructor to accept a const char* argument:
It worked as you told, but how and why it was giving error when i was simply using char, and how i will know in which conditions to use const char ? please can you elaborate
Always use
const
for pointer and reference parameters when the content is not modified by a function. You have to use it when passing a string literal because that is by definitionconst
(a"string literal"
is aconst char*
which can't be assigned to a non-constchar*
). -
#include
#includeusing namespace std;
class String{
char *name;
int length;public:
String(){ //default constructor
length = 0;
name = new char[length + 1];
}
String(char *s) //parameterised constructor
{
length = strlen(s);
name = new char[length + 1] ;strcpy(name, s); } void display(void){ cout<
i should get output :
joseph
louis
lagrange
Joseph Louis
Joseph louis lagrangebut instead i am getting:
Joseph
louis
lagrange
louis
lagrangeTarun Jha wrote:
length = a.length + b.length ;
[Trivial] note that this actually should be:
length = a.length + b.length - 1;
(And please add a destructor. As a pure FYI, since I assume this is for learning hence not using std::string, you could use std::unique_ptr for name, avoiding the need for anything but a default destructor.)
-
Always use
const
for pointer and reference parameters when the content is not modified by a function. You have to use it when passing a string literal because that is by definitionconst
(a"string literal"
is aconst char*
which can't be assigned to a non-constchar*
).