я должен был получить выход так:не жизнеспособны конструктор копирования массива элементов типа «MyString»
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;
}
Я ссылается на несколько примерных код, все из которых предполагают, что это должно работать. Тем не менее, оказалось, что, когда я пытался г ++ этот исходный код, произошла ошибка ...
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.
Пытались выяснить это на всю ночь, но безуспешно.
Заранее спасибо.
'QSort (SArray, 4, SizeOf (MyString), CompareString) «Нет, нет, тысячу раз, нет. Вы не можете использовать 'qsort' для типа, который не является POD. Используйте 'std :: sort'. * Попытался выяснить это всю ночь, но тщетно. * - Весь вопрос об использовании 'qsort' был неправильным для того, чтобы быть с. Вы потратили много времени на то, что не сработало. – PaulMcKenzie