2016-09-26 12 views
1

動的に割り当てられた配列のコピー関数を書き込もうとしています。C++ - 動的文字列配列の代入なしでコピー関数を作成

#include "StringSet.h" 
#include <iostream> 
#include <utility> 



StringSet::StringSet(int capacity) 
: arrSize{capacity}, 
    arr{make_unique<string[]>(capacity)} 
{ 
} 

StringSet::StringSet(const StringSet& a) 
{ 
    auto a2 = StringSet(currentSize); 

    for (auto i=0; i < currentSize ; i++) 
     { 
     a2[i] = a[i]; 
     } 
} 

コンパイラエラー:

error: constructors may not be cv-qualified 
error: no match for 'operator=' (operand types are 'StringSet' and 'std::string {aka std::basic_string<char>}') 
error: passing 'const StringSet' as 'this' argument discards qualifiers [-fpermissive] 
error: use of deleted function 'StringSet& StringSet::operator=(const StringSet&)' 

私の割り当ては、代入演算子をオーバーロードしています=私は私の実装ファイルで

#include <memory> 
#include <string> 


using std::string; 
using std::unique_ptr; 
using std::make_unique; 

class StringSet{ 
public: 
    //create an empty set 
    StringSet() = default; 
    StringSet(int capacity); 

    //copy a set 
    StringSet(const StringSet&); 

    StringSet& operator[](const int); 

    //Insert a string to the set 
    bool insert(string); 

    //Remove a string from the set 
    bool remove(string); 

    //Test whether a string is in the set 
    int find(string) const; 

    //Get the size of the set 
    int size() const; 

    //get string at position i 
    string get(int i) const; 

    //Return the set union of the set and another StringSet 
    StringSet setunion(const StringSet&) const; 

    //Return the intersection of the set and another StringSet 
    StringSet intersection(const StringSet&) const; 

    //Return the set diffference of the set and another StringSet 
    StringSet difference(const StringSet&) const; 

    //prevent default copy assignment 
    StringSet& operator=(const StringSet&) = delete; 

    int NOT_FOUND = -1; 
    static constexpr int def_capacity {4}; 

private: 
    int arrSize {def_capacity}; 
    int currentSize {0}; 
    unique_ptr<string[]> arr {make_unique<string[]>(def_capacity)}; 

}; 

:私は私のヘッダファイルで

私はここでそれを使用することができません。代入演算子を使用せずにコピー関数を実装する別の方法がありますか?このようにコンテンツを簡単にコピーできるstd :: stringには何かがありますか?

詳細については、ここに追加する必要があるものがあれば教えてください。

ありがとうございます。

+0

"string&operator [](const int);" "StringSet&operator [](const int);"の代わりに?また、 "const string&operator [](const int)const;"を定義する必要があります。 –

答えて

1

このコードの問題:

StringSet::StringSet(const StringSet& a) 
{ 
    auto a2 = StringSet(currentSize); 

    for (auto i=0; i < currentSize ; i++) 
    { 
     a2[i] = a[i]; 
    } 
} 

はそれがコンパイルされた場合でも、あなたが実際にthisのメンバーを初期化することはありませんしている...あなたは、いくつかの一時的なa2を初期化している、ということであるスコープの外に出ることコンストラクタの終わりに。あなたが実際にしたい:

StringSet::StringSet(const StringSet& a) 
    : StringSet(a.arrSize) 
{ 
    currentSize = a.currentSize; 

    for (auto i=0; i < currentSize; i++) 
    { 
     arr[i] = a.arr[i]; 
    } 
} 

また、あなたのoperator[]戻っそれはおそらくstd::string&を返す必要がありStringSet&を。

また、実行中のようにグローバル名前空間に名前を持たないようにする必要があります。それを地元に保つ。 std::を書くことは負担ではありません。

+0

'auto a2 = StringSet(currentSize);'は無限ループになります。 – Jarod42

+0

さて、私は実際にarrSizeが必要なときに 'auto a2 = StringSet(currentSize)'をするのは間違っていました。 私はarrがコピー関数で初期化された配列のローカルインスタンスを参照する方法を知っていませんでした。それは間違いなくいくつかの問題を解決します。 'エラー:コンストラクタはCV修飾されていない可能性があります。どういう意味ですか? – TigerCode

+0

@TigerCodeあなたの質問には含まれていないコンストラクタは、cv-qualifiedであると推測しています。それは許可されていません。 – Barry

関連する問題