繁体   English   中英

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

[英]C++ - String Array Parameter for SetUnion function

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

StringUnion了几个编译错误,我相信部分原因是我制作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&)'

可以按照预期的方式插入和查找工作,我能够在remove函数和其他一些函数中使用插入和查找函数,所以为什么不能在此处使用它们?

在你的行

StringSet StringUnion = make_unique<string[]>(arrSize);

RHS使用带有std::size_t的c ++ 14 构造,并返回内部指向数组的std::unique_ptr<std::string>

但是,LHS是StringSet对象。

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

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

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 但是您的函数会findinsert要求输入std::string 由于没有构造函数可以提供从StringSetstd::string隐式转换,因此编译器会抱怨。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM