動的に割り当てられた配列のコピー関数を書き込もうとしています。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には何かがありますか?
詳細については、ここに追加する必要があるものがあれば教えてください。
ありがとうございます。
"string&operator [](const int);" "StringSet&operator [](const int);"の代わりに?また、 "const string&operator [](const int)const;"を定義する必要があります。 –