-1

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&)'|
TigerCode
  • 315
  • 3
  • 15
  • You need to overload the `+` operator. – macroland Sep 26 '16 at 02:21
  • 1
    No, you don't need to overload the `+` operator. Whatever container you're using in the `StringSet` class for the "dynamically allocated string array", you need to initialize it, in the copy constructor, to `arr.size()`, then pass `arr`'s container's beginning iterator, ending iterator, and the `this`'s iterator to std::copy. – Sam Varshavchik Sep 26 '16 at 02:24
  • 1
    Even ignoring the fact your code is syntactically incorrect, you've left too much information out for people to sensibly help you. What does the constructor that accepts a size do? What does `copy()` do for a `StringSet`? What result do you expect to get by adding a size to a `StringSet`? – Peter Sep 26 '16 at 02:40
  • @Peter Thank you, I'll include that info right now. – TigerCode Sep 26 '16 at 02:44
  • Implement your copy constructor in a differnt way than by `copy(arr, arr + size(), a2);` – M.M Sep 26 '16 at 02:50
  • @M.M I tried using a for loop approach but my compiler said I wasn't able to use this implementation `a2[i] = arr[i]` , I thought in std::string we were allowed to assign strings to strings as such? – TigerCode Sep 26 '16 at 03:01
  • @TigerCode `std::string` doesn't appear anywhere in your code ... you have to post things that you want comments on – M.M Sep 26 '16 at 03:02
  • What do you expect `a2[i]` to do, given that you did not define `operator[]`? – M.M Sep 26 '16 at 03:32
  • You make life hard for youserlf by using `arr` both as the name of a class member, and as the name of a function parameter – M.M Sep 26 '16 at 03:33
  • You're right, I was working on overloading that operator. `StringSet& operator[](const int);` , I am currently trying to test to see if this works – TigerCode Sep 26 '16 at 03:38
  • renamed `arr` to `a` in the copy function as well. – TigerCode Sep 26 '16 at 03:42

1 Answers1

1

You need to overload the + operator, roughly:

class StringSet{
  public:
     StringSet(const StringSet&);
     StringSet& operator+(const StringSet& , int);

BTW, if your class could support both input and output iterators then you could simply use std::copy(arr.first(), arr.last(), a2.first()) which would be better of course

JamesWebbTelescopeAlien
  • 3,547
  • 2
  • 30
  • 51
  • I was going to do this - but was wondering if there was another way to do this. Seems like it might be the only solution to this problem. Thank you for your answer :) – TigerCode Sep 26 '16 at 02:25
  • @TigerCode If you think this is the right code then please accept the answer by clicking check mark right on the left side below upvote – JamesWebbTelescopeAlien Sep 26 '16 at 02:29
  • I'm looking for a solution where I don't have to use std::copy to copy the array contents. – TigerCode Sep 26 '16 at 03:08