나는 운동을하고 문제가있는 것을 시도한다.과부하 연산자 + 숯
operator +에 char을 오버로드하는 방법을 모르겠습니다.
cout<<"Please enter here a title: "<<endl;
cin>>s2;
s1="I am a " + s2;
String 클래스의 민간 부분 :
목적은 그 방정식을 해결하는 것입니다
String operator+(const String &st1, const String &st2)
{ String sum;
int len1=std::strlen(st1.str);
int len2=std::strlen(st2.str);
int lenges=len1+len2;
sum.str=new char[lenges+1];
char *a=st1.str;
char *b=st2.str;
while(*a++) {*a++;}
while(*b++) {*a=*b;}
sum.str=st1.str;
return sum.str;
}
: 나는 다음 코드를 시도하지만 작업을 나던
private:
char * str; // pointer to string
int len; // length of string
static int num_strings; // number of objects
static const int CINLIM = 80;
팁을 줄 수 있습니까?
P. String 클래스는 char * str 및 int len을 포함합니다.
"Hello"나 "I am"중 어느 것도'String'이 아니므로 두 개의'String'을 취하는'operator +'는 적용 할 수 없습니다. 좀 더 일반적으로, 근본적인 타입에 대해서만 작동하는 연산자를 오버로드 할 수 없습니다 (헤더를 포함하고'2 + 2! = 4'로 끝낼 수 있다면 악몽이 될 것입니다). 어쨌든, 여기서 방정식을 푸는 것이 무슨 뜻입니까? –
'String' 클래스를 보여주십시오. – wally