0

2 つのセットの結合を計算する関数を作成しました。

いくつかのコンパイル エラーが発生しています。これは、StringUnion配列を作成して宣言した方法が原因の 1 つだと思いますが、これまでのところ何も機能していません。

これは私のヘッダーファイルです。

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

挿入と検索は意図したとおりに機能し、削除機能やその他の機能内で挿入と検索機能を使用できたのに、ここでそれらを使用できないのはなぜですか?

4

2 に答える 2

1

あなたのラインで

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

RHS は、 を受け取る c++14コンストラクトを使用し、配列を内部的に指す をstd::size_t返しますstd::unique_ptr<std::string>

ただし、LHS はStringSetオブジェクトです。

そのような型をとるコンストラクターを定義していないので、問題です。

コードを見るとStringSetstd::unique_ptr<std::string>メンバーがあるため、そのようなオブジェクトを取得してメンバーを初期化する ctor を追加できます。ただし、すでに ctor を持っているため、そのような ctor の利点が何であるかは不明です。

StringSet(int capacity);

すでに本質的に同じことをしています。

レオンが書いているように、あなたが持っている行の代わりにこれを使うべきです

StringSet StringUnion(arrSize);
于 2016-09-29T07:06:01.343 に答える