首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++ -用于SetUnion函数的字符串数组参数

C++ -用于SetUnion函数的字符串数组参数
EN

Stack Overflow用户
提问于 2016-09-29 06:38:21
回答 2查看 159关注 0票数 0

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

我遇到了几个编译错误,我相信这在一定程度上是因为我是如何创建StringUnion数组并声明它的,但到目前为止,我所做的一切都没有起作用。

这是我的头文件。

代码语言:javascript
复制
#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函数的实现。

代码语言:javascript
复制
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}
    }
}

错误:

代码语言:javascript
复制
|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&)'

插入和查找工作,我可以在我的删除函数和其他一些函数中使用插入和查找函数,那么为什么不能在这里使用它们呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-29 07:06:01

在你的行里

代码语言:javascript
复制
StringSet StringUnion = make_unique<string[]>(arrSize);

RHS使用c++14 内部指向数组

然而,LHS是一个StringSet对象。

您没有定义接受这种类型的构造函数,所以这是一个问题。

查看您的代码,StringSet确实有一个std::unique_ptr<std::string>成员,因此您可以添加一个接受这样一个对象的ctor,并从它初始化成员。但是,目前还不清楚这样一个ctor的好处是什么,因为您已经有了一个ctor。

代码语言:javascript
复制
StringSet(int capacity);

本质上也是如此。

正如里昂所写的,你应该使用这个,而不是你的行。

代码语言:javascript
复制
StringSet StringUnion(arrSize);
票数 1
EN

Stack Overflow用户

发布于 2016-09-29 07:14:36

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

  • std::make_unique ... 从转换为非标量类型的 requested

这是因为函数std::make_unique的定义,它返回 a std::unique_ptr<T>。但是您正在尝试将其赋值为StringSet类型的值。没有从StringSet创建std::unique_ptr的构造函数或操作符,所以编译器抱怨他不能这样做。

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

StringSet有一个operator[],它在StringSet上返回一个引用,因此auto s = Array2[i];StringSet类型的。但是您的函数findinsert需要一个std::string。由于没有构造函数可以提供从StringSetstd::string的隐式转换,所以编译器会发出抱怨。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39763094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档