2016-12-10 4 views
-2

型「のMyString」のない実行可能なコンストラクタコピー配列要素:は、私はこのような出力を得ることになっていた

#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; 
} 

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- 

は、だから私はこれを書いた...私はいくつかの例示的に参照しませんコードで、これはうまくいくはずです。しかし、それが判明、私は、このソースコード++ 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. 

は全体の夜のためにこれを理解しようとしましたが、無駄に。

ありがとうございます。

+2

'のqsort(SArray、4、はsizeof(文字列MyString)、compareStringより)で動作する方法を定義させて頂いておりましたソリューション; 'いいえ、いいえ、千回、いいえ。 PODでない型に対して 'qsort'を使うことはできません。 'std :: sort'を使います。 *一晩中これを理解しようとしましたが、無駄でした。* 'qsort 'を使用することの全問題は間違っていました。あなたは、動作しないものを使って多くの時間を無駄にしました。 – PaulMcKenzie

答えて

0

リテラルを取る移動コンストラクタまたはコピーコンストラクタを定義していないため、{"string1","string2"}イニシャライザ構文を使用することはできません。おそらく

MyString[] MyStringListCreator(string list[])

のようなコンストラクタを作成し、その後

MyString list[] = MyStringListCreator({"string1","string2"})

EDIT使用する必要があります。もう少し説明を。

本質的に"Literal Constructor"MyStringのために定義されていません。だから、あなたが定義されていない文字列リテラルとMyStringを構築するための文字列です

MyString SArray[4] = {"big","me","about","take"}

言うとき。

私は基本的にあなたが文字列のtemporaily配列を作成し、そのリストの構文を使用して、あなたが機能

関連する問題