简体   繁体   English

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

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

I wrote a function to compute the union of two sets. 我写了一个函数来计算两个集合的并集。

I'm running into several compilation errors and I believe that's in part due to how I made the StringUnion array and declared it but nothing I do is working thus far. StringUnion了几个编译错误,我相信部分原因是我制作StringUnion数组并声明它的方式,但是到目前为止我什么都没做。

This is my header file. 这是我的头文件。

#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

And this is my implementation of my SetUnion function. 这是我对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}
    }
}

Errors: 错误:

|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&)'

Insert and find work as intended to and I was able to use insert and find functions within my remove function and some others so why can't I use them here? 可以按照预期的方式插入和查找工作,我能够在remove函数和其他一些函数中使用插入和查找函数,所以为什么不能在此处使用它们?

In your line 在你的行

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

The RHS uses the c++14 construct that takes an std::size_t , and returns an std::unique_ptr<std::string> internally pointing to an array . RHS使用带有std::size_t的c ++ 14 构造,并返回内部指向数组的std::unique_ptr<std::string>

The LHS, however, is a StringSet object. 但是,LHS是StringSet对象。

You did not define a constructor taking such a type, so it's a problem. 您没有定义采用这种类型的构造函数,所以这是一个问题。

Looking at your code, StringSet does have a std::unique_ptr<std::string> member, so you could add a ctor taking such an object, and initializing the member from it. 查看您的代码, StringSet确实有一个std::unique_ptr<std::string>成员,因此您可以添加一个带有此类对象的ctor,并从中初始化该成员。 However, it's unclear what would be the benefit of such a ctor, as you already have a ctor 但是,目前尚不清楚这样的ctor有什么好处,因为您已经有ctor

StringSet(int capacity);

which already essentially does the same. 基本上已经做到了

As Leon writes, you should just use this one instead of the line you have 正如Leon所写,您应该只使用这一行而不是一行

StringSet StringUnion(arrSize);

The errors provides by your compiler seem pretty clear. 您的编译器提供的错误似乎很清楚。 Let's check them. 让我们检查一下。

  • conversion from std::make_unique ... to non-scalar type StringSet requested std::make_unique ...转换为非标量类型StringSet

It's because of the definition of the function std::make_unique , which returns a std::unique_ptr<T> . 这是因为函数std::make_unique的定义,该函数返回 std::unique_ptr<T> But you're trying to assign it to a value of type StringSet . 但是,您试图将其分配给StringSet类型的值。 There is no constructor or operator for creating a StringSet from a std::unique_ptr , so the compiler complains that he can't do that. 没有从std::unique_ptr创建StringSet构造函数或运算符,因此编译器抱怨他不能这样做。

  • error: no matching function for call to 'StringSet::find(StringSet&)' 错误:没有匹配的函数来调用'StringSet::find(StringSet&)'

Your class StringSet has an operator[] that returns a reference on a StringSet so auto s = Array2[i]; 您的类StringSet有一个operator[] ,它返回StringSet上的引用,因此auto s = Array2[i]; is of type StringSet . 类型为StringSet But your functions find and insert ask for a std::string . 但是您的函数会findinsert要求输入std::string As there is no constructor that can provide implicit conversion from StringSet to std::string , the compiler complains. 由于没有构造函数可以提供从StringSetstd::string隐式转换,因此编译器会抱怨。

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

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