C++ 在C+中使用集合和重载运算符+;

C++ 在C+中使用集合和重载运算符+;,c++,C++,因此,在本周的课堂上,我必须使用集合从.txt文件中读取《独立宣言》和《美国宪法》,将它们存储在集合中,并重载操作符*以查找并返回两个集合的交点 阅读中的所有内容都不是问题,找出集合中的交集也不是问题。我遇到的很多问题是重载操作符*。我一直有两个错误: 没有与这些操作数匹配的运算符“*” 及 binary“*”:'std::set'未定义此运算符或到预定义运算符可接受的类型的转换 以下是我目前的代码: 从main: Reader r; std::set<std::string> d,

因此,在本周的课堂上,我必须使用
集合
.txt
文件中读取《独立宣言》和《美国宪法》,将它们存储在集合中,并重载
操作符*
以查找并返回两个
集合
的交点

阅读中的所有内容都不是问题,找出集合中的交集也不是问题。我遇到的很多问题是重载
操作符*
。我一直有两个错误:

没有与这些操作数匹配的运算符“*”

binary“*”:'std::set'未定义此运算符或到预定义运算符可接受的类型的转换

以下是我目前的代码:

从main:

Reader r;
std::set<std::string> d, c;
d = r.GetDeclaraton();
c = r.GetConstitution();
Set dec(d), con(c);
Set intersection = dec * con;
你需要

Set operator* (const Set &rhs) const
因为这是一个二进制运算符,有两个
Set
s作为参数。只有当您的
Set
是派生
std::Set
时,您的尝试才会起作用

这里的返回类型是
Set
。与输入参数相同更有意义,这也与您在
main
中的用法一致。因此,在这种情况下,您需要修改定义以构造并返回
Set
,而不是
std::Set
(或者您不需要,因为
std::Set
可以隐式转换为
Set

注意,我还创建了
operator*
一个
const
成员函数,因为它没有修改它的对象。

您需要

Set operator* (const Set &rhs) const
因为这是一个二进制运算符,有两个
Set
s作为参数。只有当您的
Set
是派生
std::Set
时,您的尝试才会起作用

这里的返回类型是
Set
。与输入参数相同更有意义,这也与您在
main
中的用法一致。因此,在这种情况下,您需要修改定义以构造并返回
Set
,而不是
std::Set
(或者您不需要,因为
std::Set
可以隐式转换为
Set


请注意,我还创建了
操作符*
一个
常量
成员函数,因为它没有修改其对象。

操作符*
是在自定义
集合
中定义的,因此左侧参数必须是
集合
,但是在
dec.s*con.s
中,您正在使用
.s
访问成员
std::set
字段,并且没有为左手
set
和右手
std::set
定义
操作符*


您可以更改为
dec*con.s
,但最好将
operator*
rhs参数也更改为
const Set&
,并在操作符内部使用
rhs.s
。。。少点混乱

运算符*
在自定义
集合中定义,因此左侧参数必须是
集合
,但在
dec.s*con.s
中,您使用的是访问成员
std::Set
字段的
.s
,对于左手
Set
和右手
std::Set
没有定义
操作符*


您可以更改为
dec*con.s
,但最好将
operator*
rhs参数也更改为
const Set&
,并在操作符内部使用
rhs.s
。。。少点混乱

在我看来,最好编写一个全局运算符或友元运算符。此外,对于交叉点,我将使用
&
操作符,对于联合
|
+
操作符,而差分:
-

为什么是全球运营商?使用全局运算符,即使左操作数不是类,也可以为类编写运算符。例如,对于矩阵类,您可以轻松编写一个全局运算符,将浮点和矩阵相乘,而作为矩阵成员运算符,您只能在矩阵位于左侧的位置编写一个运算符

例如:

template <typename T>
inline std::set<T> intersection(const std::set<T>& smaller, const std::set<T>& larger)
{
    std::set<T> result;
    for (auto it=smaller.begin(),eit=smaller.end(); it!=eit; ++it)
    {
        if (larger.find(*it) != larger.end())
            result.insert(*it);
    }
    return result;
}

template <typename T>
inline std::set<T> operator & (const std::set<T>& a, const std::set<T>& b)
{
    if (a.size() < b.size())
        return intersection(a, b);
    return intersection(b, a);
}

class Set
{   
public:
    std::set<std::string> s;
    Set() {}
    Set(std::set<std::string> _s) : s(_s) {}

    friend Set operator & (const Set& a, const Set& b)
    {
        return Set(a.s & b.s);
    }
};

int test()
{
    std::set<std::string> s, t;
    s.insert("aa");
    s.insert("bb");
    t.insert("bb");
    t.insert("cc");
    std::set<std::string> u(s & t);

    Set ss(s), st(t);
    Set result(ss & st);
    return 0;
}
模板
内联标准::集合交叉点(常数标准::集合和较小,常数标准::集合和较大)
{
std::设置结果;
for(auto it=small.begin(),eit=small.end();it!=eit;++it)
{
if(更大的.find(*it)!=更大的.end())
结果。插入(*it);
}
返回结果;
}
模板
内联std::set运算符&(常量std::set&a,常量std::set&b)
{
如果(a.size()
在我看来,最好编写一个全局运算符或友元运算符。此外,对于交叉点,我将使用
&
操作符,对于联合
|
+
操作符,而差分:
-

为什么是全球运营商?使用全局运算符,即使左操作数不是类,也可以为类编写运算符。例如,对于矩阵类,您可以轻松编写一个全局运算符,将浮点和矩阵相乘,而作为矩阵成员运算符,您只能在矩阵位于左侧的位置编写一个运算符

例如:

template <typename T>
inline std::set<T> intersection(const std::set<T>& smaller, const std::set<T>& larger)
{
    std::set<T> result;
    for (auto it=smaller.begin(),eit=smaller.end(); it!=eit; ++it)
    {
        if (larger.find(*it) != larger.end())
            result.insert(*it);
    }
    return result;
}

template <typename T>
inline std::set<T> operator & (const std::set<T>& a, const std::set<T>& b)
{
    if (a.size() < b.size())
        return intersection(a, b);
    return intersection(b, a);
}

class Set
{   
public:
    std::set<std::string> s;
    Set() {}
    Set(std::set<std::string> _s) : s(_s) {}

    friend Set operator & (const Set& a, const Set& b)
    {
        return Set(a.s & b.s);
    }
};

int test()
{
    std::set<std::string> s, t;
    s.insert("aa");
    s.insert("bb");
    t.insert("bb");
    t.insert("cc");
    std::set<std::string> u(s & t);

    Set ss(s), st(t);
    Set result(ss & st);
    return 0;
}
模板
内联标准::集合交叉点(常数标准::集合和较小,常数标准::集合和较大)
{
std::设置结果;
for(auto it=small.begin(),eit=small.end();it!=eit;++it)
{
if(更大的.find(*it)!=更大的.end())
结果。插入(*it);
}
返回结果;
}
模板
内联std::set运算符&(常量std::set&a,常量std::set&b)
{
如果(a.size()