C++ C++;尝试在模板化类中重载/运算符时出错

C++ C++;尝试在模板化类中重载/运算符时出错,c++,operator-overloading,template-classes,C++,Operator Overloading,Template Classes,我正在尝试为我创建的模板类重载“+”、“-”和“/”运算符。 +和-运算符工作得很好,但是/运算符重载给了我错误 我正在使用Visual Studio 2013 //Set.h #pragma once #include <iostream> #include <vector> using namespace std; template<class T> class Set { friend Set<T> operator+ &l

我正在尝试为我创建的模板类重载“+”、“-”和“/”运算符。 +和-运算符工作得很好,但是/运算符重载给了我错误

我正在使用Visual Studio 2013

//Set.h 
#pragma once
#include <iostream>
#include <vector>
using namespace std;

template<class T>
class Set
{   
    friend Set<T> operator+ <> (const Set& left, const Set& right);  //works
    friend Set<T> operator- <> (const Set& left, const Set& right);  //works
    friend Set<T> operator/ <> (const Set& left, const Set& right);  //does not

public:
    Set(int n)
    {
        numItems = n;
        setItems();
    }
    Set()
    {
        numItems = 0;
        setItems();
    }
    ~Set();
    void setItems();
    void output();
private:
    int numItems;
    vector<T> set;
};
//Set.h
#布拉格语一次
#包括
#包括
使用名称空间std;
模板
类集
{   
友元集运算符+(常量集&左,常量集&右);//有效
朋友集操作符-(常量集&左,常量集&右);//有效
友元集运算符/(常数集&左,常数集&右);//不
公众:
集合(整数n)
{
numItems=n;
setItems();
}
集合()
{
numItems=0;
setItems();
}
~Set();
void setItems();
无效输出();
私人:
int numItems;
向量集;
};
我想重载/操作员以确定

定义:

    //---------------- Overload intersection -----------------
template<class T>
Set<T> operator/(const Set<T>& left, const Set<T>& right)
{
    bool putin = false;
    Set<T> quotient;

    // loops through left Set
    for (int i = 0; i < left.set.size(); i++)
    {
        for (int j = 0; j < right.set.size(); j++)
            //loops through right set
        {
            //checks if the item in left is in right set
            // if it is, PUT IT IN the new set
            if (left.set[i] == right.set[j])
                putin = true;
        }
        if (putin)
            quotient.set.push_back(left.set[i]);
        putin = true;
    }

    return quotient;
} 
/--------------超载交叉口-----------------
模板
集合运算符/(常数集和左、常数集和右)
{
布尔·普京=错误;
集商;
//循环通过左集合
对于(int i=0;i
下面是我得到的错误


错误1错误C2143:语法错误:缺少“;”在“之前,这是一个棘手的问题,它包含在一个条目中

我不能确切地解释为什么你的编译器会给出错误。但是您为所有运算符编写的代码都不正确:正如常见问题解答中所解释的,编译器认为您试图将非模板函数设置为
友元
,但这无法工作,因为这些函数确实需要是模板函数才能接受

常见问题解答建议的解决方案是先声明模板函数,然后再添加它们:

template<typename T> class Set;

template<typename T>
Set<T> operator+ (const Set<T>& left, const Set<T>& right);
template<typename T>
Set<T> operator- (const Set<T>& left, const Set<T>& right); 
template<typename T>
Set<T> operator/ (const Set<T>& left, const Set<T>& right); 

template<class T>
class Set
{   
    friend Set<T> operator+ <> (const Set<T>& left, const Set<T>& right);
    friend Set<T> operator- <> (const Set<T>& left, const Set<T>& right);
    friend Set<T> operator/ <> (const Set<T>& left, const Set<T>& right);
其中,您明确地将模板函数称为朋友。我不完全确定第二种方法是否是个好主意

如果您的操作符具有“正常”语义,那么也许您可以一起避免这个问题,但根本不使用
friend
s。如果定义成员函数
operator+=
operator-=
operator/=
,这些函数在
*此
上运行,那么在类外声明其他操作符就很简单了,这些操作符无需成为好友即可委托给这些函数:

template<typename T> 
Set<T> operator+ (Set<T> a, Set<T> const &b} { return a += b; }
模板
集合运算符+(集合a,集合常量&b}{返回a+=b;}
template<typename T> 
Set<T> operator+ (Set<T> a, Set<T> const &b} { return a += b; }