So I'm trying to write a copy function that copies all the elements of a dynamically allocated string array.
In my header file I have it defined as having the following type/return values:
#include <algorithm>
#include <string>
using std::string
using std::copy
class StringSet{
public:
StringSet(const StringSet&);
For implementation I have:
StringSet::StringSet(const StringSet& arr)
{
auto a2 = StringSet(size());
copy(arr,arr + size(), a2);
}
where size() returns the current size of the string array. I also have this restriction on the operator=
//prevent default copy assignment
StringSet& operator=(const StringSet&) = delete;
Since I haven't defined the operator+ as being part of the class and have a restriction for the operator= to not be included, I've ran into a problem.
The obvious issue here is that I get the error:
error: no match for 'operator+' (operand types are 'const StringSet' and 'int')
How should I go about this error without using + or = operators?
The StringSet constructor initializes a dynamically allocated string array of size 'capacity'
StringSet::StringSet(int capacity)
: arrSize{capacity},
arr{make_unique<string[]>(capacity)}
{
}
The Copy constructor is supposed to create a deep copy of its parameter.
My understanding is that I need to supply std::copy with the beg the source + beginning iterator, source + ending iterator and the destination + beginning iterator as its arguments for it to deep copy.
However, I'd like to not use std::copy at all for this. How would implementation for the for loop for deep copying look like in this case?
I've tried writing a for loop but I'm getting a compiler error for the operator[]
StringSet::StringSet(const StringSet& a)
{
auto a2 = StringSet(currentSize);
for (auto i=0; i < currentSize ; i++ )
{
a2[i] = a[i];
}
}
The error
error: no match for 'operator[]' (operand types are 'StringSet' and 'int')|
error: no match for 'operator[]' (operand types are 'const StringSet' and 'int')|
Edit:
I've overloaded operator[] as such:
StringSet& operator[](const int);
And this is the new error
error: passing 'const StringSet' as 'this' argument discards qualifiers [-fpermissive]|
error: use of deleted function 'StringSet& StringSet::operator=(const StringSet&)'|