I wrote a function to compute the union of two sets.
I'm running into several compilation errors and I believe that's in part due to how I made the StringUnion
array and declared it but nothing I do is working thus far.
This is my header file.
#ifndef StringSet_header
#define StringSet_header
#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)};
};
#endif
And this is my implementation of my SetUnion
function.
StringSet StringSet::setunion(const StringSet &Array2) const
{
StringSet StringUnion = make_unique<string[]>(arrSize);
if (currentSize > 0)
{
for (auto i=0; i < currentSize; i++)
{
auto s = arr[i];
StringUnion.insert(s);
}
for (auto i=0; i < Array2.currentSize; i++)
{
auto s = Array2[i];
if (StringUnion.find(s) == NOT_FOUND)
{
StringUnion.insert(s);
}
}
}
else
{
auto result = StringSet();
return result; //return empty StringSet}
}
}
Errors:
|error: conversion from 'std::_MakeUniq<std::basic_string<char> []>::__array {aka std::unique_ptr<std::basic_string<char> []>}' to non-scalar type 'StringSet' requested|
error: passing 'const StringSet' as 'this' argument discards qualifiers [-fpermissive]
error: no matching function for call to 'StringSet::find(StringSet&)'
error: no matching function for call to 'StringSet::insert(StringSet&)'
Insert and find work as intended to and I was able to use insert and find functions within my remove function and some others so why can't I use them here?
In your line
StringSet StringUnion = make_unique<string[]>(arrSize);
The RHS uses the c++14 construct that takes an std::size_t
, and returns an std::unique_ptr<std::string>
internally pointing to an array.
The LHS, however, is a StringSet
object.
You did not define a constructor taking such a type, so it's a problem.
Looking at your code, StringSet
does have a std::unique_ptr<std::string>
member, so you could add a ctor taking such an object, and initializing the member from it. However, it's unclear what would be the benefit of such a ctor, as you already have a ctor
StringSet(int capacity);
which already essentially does the same.
As Leon writes, you should just use this one instead of the line you have
StringSet StringUnion(arrSize);