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
soauto 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
soauto s=Array2[i]的引用代码>的类型为StringSet
。但是您的函数find
和insert
要求输入std::string
。由于没有构造函数可以提供从StringSet
到std::string
的隐式转换,编译器会抱怨。初始化StringUnion
,如下所示:StringSet StringUnion(arrSize)代码>初始化StringUnion
如下:StringSet StringUnion(arrSize)代码>所以通过修改我的.h文件的运算符[]来返回字符串,这应该可以解决问题?如果它是一个引用,那么只返回字符串值就可以了。因此,通过修改我的.h文件的运算符[]来返回字符串,这应该可以解决问题?如果它是一个引用,那么只返回字符串值就可以了。