我应该得到这样的输出:
1. abcd-efgh-abcd-
2. abcd-
3.
4. abcd-efgh-
5. efgh-
6. c
7. abcd-
8. ijAl-
9. ijAl-mnop
10. qrst-abcd-
11. abcd-qrst-abcd- uvw xyz
about
big
me
take
abcd
qrst-abcd-
所以我写了这个......
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
// ------------------MY CODE HERE-----------------
class MyString : public string {
public:
MyString():string(){}
MyString(const string &s):string(s){}
MyString(const char *c):string(c){}
MyString(MyString &ms):string(ms){}
string operator()(int start_, int length_){
return this->substr(start_, length_);
}
};
// -----------------------------------------------
int CompareString( const void * e1, const void * e2) {
MyString * s1 = (MyString * ) e1;
MyString * s2 = (MyString * ) e2;
if( *s1 < *s2 ) return -1;
else if( *s1 == *s2 ) return 0;
else if( *s1 > *s2 ) return 1;
}
int main() {
MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
MyString SArray[4] = {"big","me","about","take"};
cout << "1. " << s1 << s2 << s3<< s4<< endl;
s4 = s3; s3 = s1 + s3;
cout << "2. " << s1 << endl;
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl;
s2 = s1; s1 = "ijkl-";
s1[2] = 'A' ;
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";
cout << "9. " << s1 << endl;
s4 = "qrst-" + s2;
cout << "10. " << s4 << endl;
s1 = s2 + s4 + " uvw " + "xyz";
cout << "11. " << s1 << endl;
qsort(SArray,4,sizeof(MyString), CompareString);
for( int i = 0;i < 4;++i )
cout << SArray[i] << endl;
//use string substr(int start,int length);
cout << s1(0,4) << endl;
//use string substr(int start,int length);
cout << s1(5,10) << endl;
return 0;
}
我引用了几个示例代码,所有代码都表明这应该有效。然而事实证明,当我尝试g ++这个源代码时,发生错误......
maGicZ:PA3 MagicZ$ g++ part_1.cpp
part_1.cpp:23:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
part_1.cpp:26:27: error: no viable constructor copying array element of type 'MyString'
MyString SArray[4] = {"big","me","about","take"};
^~~~~
part_1.cpp:11:2: note: candidate constructor not viable: expects an l-value for 1st argument
MyString(MyString &ms):string(ms){};
^
part_1.cpp:26:33: error: no viable constructor copying array element of type 'MyString'
MyString SArray[4] = {"big","me","about","take"};
^~~~
part_1.cpp:11:2: note: candidate constructor not viable: expects an l-value for 1st argument
MyString(MyString &ms):string(ms){};
^
part_1.cpp:26:38: error: no viable constructor copying array element of type 'MyString'
MyString SArray[4] = {"big","me","about","take"};
^~~~~~~
part_1.cpp:11:2: note: candidate constructor not viable: expects an l-value for 1st argument
MyString(MyString &ms):string(ms){};
^
part_1.cpp:26:46: error: no viable constructor copying array element of type 'MyString'
MyString SArray[4] = {"big","me","about","take"};
^~~~~~
part_1.cpp:11:2: note: candidate constructor not viable: expects an l-value for 1st argument
MyString(MyString &ms):string(ms){};
^
1 warning and 4 errors generated.
试图弄清整夜,但徒劳无功。
提前致谢。
答案 0 :(得分:0)
您无法使用{"string1","string2"}
初始值设定项语法,因为您尚未定义带有文字的移动或复制构造函数。你应该做一个像
MyString[] MyStringListCreator(string list[])
然后使用
MyString list[] = MyStringListCreator({"string1","string2"})
编辑:多一点解释。
基本上"Literal Constructor"
没有为MyString
定义。所以当你说
MyString SArray[4] = {"big","me","about","take"}
你是一个字符串来构造一个MyString
,其中包含一个未定义的字符串文字。
我给出的解决方案基本上是让你使用该列表语法来创建一个临时的字符串数组,然后你定义它在函数中的工作方式