Home > Back-end >  Why can't I custom string classes statement when the assignment?
Why can't I custom string classes statement when the assignment?

Time:09-26

Myself defines a string class MyStringA, realized the "operator=" assignment operation, but encountered when using the following problems:
1, MyStringA STR (" Hello World ") is allowed, but MyStringA STR="Hello World"; Is not
2, to debug the error message is: error: invalid initialization of non - const reference of type 'MyStringA& 'from an rvalue of type' MyStringA '
Want to ask about a great god, and this is what circumstance? How to solve?

CodePudding user response:

 
The class MyStringA
{
Public:
MyStringA ();
Virtual ~ MyStringA ();

MyStringA (MyStringA& STR);
MyStringA (CHAR [] s);
MyStringA (const CHAR * pStr);


MyStringA& Operator=(MyStringA& STR);
MyStringA& Operator=(CHAR * PSTR);
MyStringA& The operator + (CHAR * PSTR);
MyStringA& Operator + (MyStringA& STR);
MyStringA& Operator +=(MyStringA& STR);
MyStringA& Operator +=(CHAR * PSTR);

Public:
CHAR * ToString ();
UINT Length ();

Protected:

Private:
CHAR * psData;
};

CodePudding user response:

 
# include & lt; iostream>
# include & lt; Cstring>
# include & lt; string>
using namespace std;
Int main ()
{
MyStringA STR (" Hello ");
CoutCoutCoutMyStringA str2 (" World ");
STR +=str2;
CoutCout
MyStringA str3="My String Class";
CoutCout//string str3="My string Class";
//coutreturn 0;
}

CodePudding user response:

MyStringA (MyStringA& STR);
Try to const
  • Related