SetUnion函数的字符串数组参数
C++ - String Array Parameter for SetUnion function
我写了一个函数来计算两个集合的并集。
我遇到了几个编译错误,我相信这部分是由于我如何制作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
这是SetUnion
函数的实现。
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}
}
}
错误:
|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&)'
插入和查找工作的预期,我能够使用插入和查找函数在我的删除函数和其他一些,所以为什么我不能在这里使用它们?
在你的行
StringSet StringUnion = make_unique<string[]>(arrSize);
RHS使用c++14结构,它接受一个std::size_t
,并返回一个内部指向数组的std::unique_ptr<std::string>
。
StringSet
对象。您没有定义接受这种类型的构造函数,因此这是一个问题。
看看你的代码,StringSet
确实有一个std::unique_ptr<std::string>
成员,所以你可以添加一个接受这样一个对象的函数,并从它初始化成员。但是,由于您已经有了一个
StringSet(int capacity);
实际上已经做了相同的事情。
正如Leon所写的,你应该使用这一行而不是你的
StringSet StringUnion(arrSize);
编译器提供的错误看起来很清楚。让我们检查一下。
-
请求
std::make_unique ...
转换为非标量类型StringSet
这是因为函数std::make_unique
的定义,它返回一个std::unique_ptr<T>
。但是你试图将它分配给StringSet
类型的值。没有构造函数或操作符可以从std::unique_ptr
创建StringSet
,所以编译器抱怨他不能这样做。
-
错误:没有匹配
'StringSet::find(StringSet&)'
的函数
你的类StringSet
有一个operator[]
,它返回对StringSet
的引用,所以auto s = Array2[i];
是StringSet
类型。但是您的函数find
和insert
需要std::string
。由于没有构造函数可以提供从StringSet
到std::string
的隐式转换,编译器会报错。
- C++函数模板需要 &for 数组参数
- C++ 数组参数不起作用
- 使用数组参数进行函数专用化
- 为什么我的数组参数的方法无法正常工作?(C )
- SWIG - Java 代理类数组参数
- 在 gcc 中使用数组参数编译外部"c"代码
- 从 VB6 调用的 C++ DLL 函数中的输出数组参数
- 如何根据数组参数项类型重载 IDL 中的函数
- 引用谷歌测试/模拟框架中的数组参数
- C++将数组参数传递给函数
- 字符星形数组参数未正确终止
- 如何将德尔菲的"字符串数组"参数翻译成C++?
- 为什么 gcc 无法推断数组参数的模板化大小?(C++11)
- 使用 const char 数组参数分析 constexpr 显示运行时执行
- C++指针数组参数
- 固定大小的多维数组参数
- 为什么数组参数被视为常量数组
- 如何在pgsql中格式化数组参数
- C++字符数组参数在x86编译中包含奇怪的字符
- 对象数组参数 - 错误:字段“字母”的类型不完整