C++ C++;-SetUnion函数的字符串数组参数

C++ C++;-SetUnion函数的字符串数组参数,c++,arrays,string,compiler-errors,set-union,C++,Arrays,String,Compiler Errors,Set Union,我写了一个函数来计算两个集合的并集 我遇到了几个编译错误,我相信这部分是由于我如何创建StringUnion数组并声明了它,但到目前为止我没有做任何工作 这是我的头文件 #ifndef StringSet_header #define StringSet_header #include <memory> #include <string> using std::string; using std::unique_ptr; using std::make_unique;

我写了一个函数来计算两个集合的并集

我遇到了几个编译错误,我相信这部分是由于我如何创建
StringUnion
数组并声明了它,但到目前为止我没有做任何工作

这是我的头文件

#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
错误:

|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&)'
|错误:请求将“std::_MakeUniq::u数组{aka std::unique_ptr}”转换为非标量类型“StringSet”|
错误:将“const StringSet”作为“this”参数传递将丢弃限定符[-fppermissive]
错误:调用“StringSet::find(StringSet&)”时没有匹配的函数
错误:调用“StringSet::insert(StringSet&)”时没有匹配的函数
按预期插入和查找工作,并且我能够在我的删除函数和其他一些函数中使用插入和查找函数,所以为什么我不能在这里使用它们?

在您的行中

StringSet StringUnion = make_unique<string[]>(arrSize);
基本上已经是这样了

正如Leon所写,你应该用这一行而不是你的那一行

StringSet StringUnion(arrSize);
你行吗

StringSet StringUnion = make_unique<string[]>(arrSize);
基本上已经是这样了

正如Leon所写,你应该用这一行而不是你的那一行

StringSet StringUnion(arrSize);

编译器提供的错误似乎非常清楚。让我们检查一下

  • std::make_unique…
    转换为请求的非标量类型
    StringSet
这是因为函数
std::make_unique
的定义,它是一个
std::unique_ptr
。但您正试图将其分配给类型为
StringSet
的值。没有用于从
std::unique\u ptr
创建
StringSet
的构造函数或运算符,因此编译器抱怨他不能这样做

  • 错误:调用
    'StringSet::find(StringSet&')时没有匹配的函数。

您的类
StringSet
有一个
操作符[]
,它返回对
StringSet
so
auto s=Array2[i]的引用的类型为
StringSet
。但是您的函数
find
insert
要求输入
std::string
。由于没有构造函数可以提供从
StringSet
std::string
的隐式转换,编译器会抱怨。

编译器提供的错误似乎非常清楚。让我们检查一下

  • std::make_unique…
    转换为请求的非标量类型
    StringSet
这是因为函数
std::make_unique
的定义,它是一个
std::unique_ptr
。但您正试图将其分配给类型为
StringSet
的值。没有用于从
std::unique\u ptr
创建
StringSet
的构造函数或运算符,因此编译器抱怨他不能这样做

  • 错误:调用
    'StringSet::find(StringSet&')时没有匹配的函数。

您的类
StringSet
有一个
操作符[]
,它返回对
StringSet
so
auto s=Array2[i]的引用的类型为
StringSet
。但是您的函数
find
insert
要求输入
std::string
。由于没有构造函数可以提供从
StringSet
std::string
的隐式转换,编译器会抱怨。

初始化
StringUnion
,如下所示:
StringSet StringUnion(arrSize)初始化
StringUnion
如下:
StringSet StringUnion(arrSize)所以通过修改我的.h文件的运算符[]来返回字符串,这应该可以解决问题?如果它是一个引用,那么只返回字符串值就可以了。因此,通过修改我的.h文件的运算符[]来返回字符串,这应该可以解决问题?如果它是一个引用,那么只返回字符串值就可以了。