Home > Back-end >  Consult a class array initialization problem
Consult a class array initialization problem

Time:09-26

MyString {
Public:
Char * STR=NULL;
MyString (char * a) {
STR=new char [strlen (a) + 1);
Strcpy (STR, a);
}
MyString (MyString & amp; A) {
STR=new char [strlen (a.s tr) + 1);
Strcpy (STR, a.s tr);
}
MyString & amp; A) operator=(char * {
If (STR) delete [] STR;
STR=new char [strlen (a) + 1);
Strcpy (STR, a);
Return * this;
}};
Int main () {
MyString array [4]={" hello ", "the", "world", "bye"};
}
In the process of compiling error "no matching function for the call to 'MyString: : MyString (MyString)'"
I want to know where is wrong?
Or is overloaded function again?

CodePudding user response:

What compile??????
With me ok?

CodePudding user response:

reference 1/f, the truth is more important than right or wrong response:
what compile??????
With me ok?

The compiler is dev - c + +, this program is a part of a program, compile the whole program will mark red errors in this profession, but separate compilation is no problem

CodePudding user response:

The nikkei, copy structure parameters and const
 
MyString (const MyString & amp; A) {

}

CodePudding user response:

Copy structure write wrong

CodePudding user response:

reference SDGHCHJ reply: 3/f
the nikkei, copy structure parameters and const
 
MyString (const MyString & amp; A) {

}
thanks, problem solved! But I would like to ask why must add const? (or if it is the request of dev)

CodePudding user response:

reference 5 floor xyx_2000 reply:
Quote: refer to the third floor SDGHCHJ response:

The nikkei, copy structure parameters and const
 
MyString (const MyString & amp; A) {

}
thanks, problem solved! But I would like to ask why must add const? (or if it is the request of dev)
you still go to check the use of the const, in particular, the role of const reference
  • Related