1

Trying to write a copy function for a dynamically allocated array.

In my header file I have:

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

};

In my implementation file I have:

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

Compiler error:

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&)'

My assignment has overloaded the assignment operator= and thus I'm not able to use that here. Is there another way of implementing a copy function without using the assignment operator - is there anything in std::string that allows us to copy contents easier in this manner?

If there's anything else I need to add here for details please let me know.

Thank you.

1
  • shouldn't it be "string& operator[](const int);" instead of "StringSet& operator[](const int);"? Also, you should define a "const string& operator[](const int) const;" Commented Sep 26, 2016 at 20:00

1 Answer 1

1

The problem with this code:

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

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

is that, even if it compiled, you're never actually initializing the members of this... you're initializing some temporary a2 that goes out of scope at the end of the constructor. You actually want:

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

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

Also, your operator[] returns a StringSet& where it should probably return a std::string&.

Also, you should avoid bringing names into the global namespace like you're doing. Keep it local. Writing std:: is not a burden.

Sign up to request clarification or add additional context in comments.

6 Comments

auto a2 = StringSet(currentSize); would even be infinite loop.
Yea I was mistaken about doing the auto a2 = StringSet(currentSize) when I actually wanted arrSize. I didn't know arr was the way of referring to the local instance of the array initialized in the copy function though. That definitely clears some of the problems up. error: constructors may not be cv-qualified What does this mean though?
@TigerCode I'm guessing some constructor you have that you didn't include in your question is cv-qualified. That's not allowed.
In the header file - for the copy function i forgot to include a place holder for the variable.
@TigerCode Comments aren't for followup questions. I have no idea what a2 or a1 are, or why you have that extra &
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.