簡體   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