gpt4 book ai didi

试图重载模板类中的/运算符的 C++ 错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:22:45 24 4
gpt4 key购买 nike

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

我正在使用 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;
};

我想重载/运算符以确定 intersection of two sets

定义:

    //---------------- 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;
}

这是我遇到的错误

Error 1 error C2143: syntax error : missing ';' before '<'

Error 2 error C2460: '/' : uses 'Set<T>', which is being defined

Error 3 error C2433: '/' : 'friend' not permitted on data declarations

Error 4 error C2238: unexpected token(s) preceding ';'

Error 5 error C2365: '/' : redefinition; previous definition was 'data variable'

Error 6 error C2904: '/' : name already used for a template in the current scope

Error 7 error C2460: '/' : uses 'Set<int>', which is being defined

最佳答案

这很棘手,它包含在 C++ FAQ 中条目。

我无法准确解释为什么您的编译器会给出错误。但是您的所有运算符的代码都不正确:如常见问题解答中所述,编译器认为您正在尝试 friend一个非模板函数,但是这不起作用,因为函数确实需要是模板函数才能接受 Set<T> .

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

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);

请注意,您需要使用 Set<T> ,不只是 Set与您的原始代码一样。这是有效的,因为现在编译器知道我们在谈论函数模板,所以它在 friend 中识别它们。行。

另一种可能的解决方案是,没有函数的前向声明:

template<class T>
class Set
{
template<class U>
friend Set<U> operator / (const Set<U>& left, const Set<U>& right);
template<class U>
friend Set<U> operator+ (const Set<U>& left, const Set<U>& right);
template<class U>
friend Set<U> operator- (const Set<U>& left, const Set<U>& right);
public:
// ...

您明确将模板函数称为 friend 的地方。我不完全确定第二种方法是个好主意。

如果您的运算符具有“正常”语义,那么也许您可以一起避免这个问题,但不要使用 friend完全没有。如果定义成员函数operator+= , operator-= , operator/=*this 上运行,那么在类之外声明其他运算符就很简单了,这些运算符委托(delegate)给那些函数而不需要成为 friend :

template<typename T> 
Set<T> operator+ (Set<T> a, Set<T> const &b} { return a += b; }

关于试图重载模板类中的/运算符的 C++ 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23576756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com