- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试为我创建的模板类重载“+”、“-”和“/”运算符。+ 和 - 运算符工作得很好,但/运算符重载给我错误。
我正在使用 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/
Or 运算符 对两个表达式进行逻辑“或”运算。 result = expression1 Or expression2 参数 result 任意数值变量。 expression1 任意
Not 运算符 对表达式执行逻辑非运算。 result = Not expression 参数 result 任意数值变量。 expression 任意表达式。 说明 下表显示如何
Is 运算符 比较两个对象引用变量。 result = object1 Is object2 参数 result 任意数值变量。 object1 任意对象名。 object2 任意
\ 运算符 两个数相除并返回以整数形式表示的结果。 result = number1\number2 参数 result 任意数值变量。 number1 任意数值表达式。 numbe
And 运算符 对两个表达式进行逻辑“与”运算。 result = expression1 And expression2 参数 result 任意数值变量。 expression1
运算符(+) 计算两个数之和。 result = expression1 + expression2 参数 result 任意数值变量。 expression1 任意表达式。 exp
我对此感到困惑snippet : var n1 = 5-"4"; var n2 = 5+"4"; alert(n1); alert(n2); 我知道 n1 是 1。那是因为减号运算符会将字符串“4”转
我想我会得到 12,而不是 7。 w++,那么w就是4,也就是100,而w++, w 将是 8,1000;所以 w++|z++ 将是 100|1000 = 1100 将是 12。 我怎么了? int
Xor 运算符 对两个表达式进行逻辑“异或”运算。 result = expression1 Xor expression2 参数 result 任意数值变量。 expression1
Mod 运算符 两个数值相除并返回其余数。 result = number1 Mod number2 参数 result 任意数值变量。 number1 任意数值表达式。 numbe
Imp 运算符 对两个表达式进行逻辑蕴涵运算。 result = expression1 Imp expression2 参数 result 任意数值变量。 expression1 任
Eqv 运算符 执行两个表达式的逻辑等价运算。 result = expression1 Eqv expression2 参数 result 任意数值变量。 expression1 任
我有一个运算符重载的简单数学 vector 类。我想为我的运算符(operator)获取一些计时结果。我可以通过计时以下代码轻松计时我的 +=、-=、*= 和/=: Vector sum; for(s
我是用户定义比较运算符的新手。我正在读一本书,其中提到了以下示例: struct P { int x, y; bool operator、运算符<等),我们
在 SQL 的维基百科页面上,有一些关于 SQL 中 bool 逻辑的真值表。 [1] 维基百科页面似乎来源于 SQL:2003 标准。 等号运算符 (=) 的真值表与 SQL:2003 草案中的 I
我遇到了一个奇怪的 C++ 运算符。 http://www.terralib.org/html/v410/classoracle_1_1occi_1_1_number.html#a0f2780081f
我正在阅读关于 SO 和 answers 中的一个问题,它被提到为: If no unambiguous matching deallocation function can be found, pr
我偶然发现了这个解决方案,但我无法理解其中到底发生了什么。谁能解释一下! 据我了解,它试图通过计算一半的单元格然后将其加倍来计算 a*b 网格中的单元格数量。但是我无法理解递归调用。 请不要建议其他解
Go的基本类型 布尔类型bool 长度:1字节 取值:布尔类型的取值只能是true或者false,不能用数字来表示 整型 通用整型 int / uint(有符号 / 无符号,下面也类似) 长度:根据运
在本教程中,您将学习JavaScript中可用的不同运算符,以及在示例的帮助下如何使用它们。 什么是运算符? 在JavaScript中,运算符是一种特殊符号,用于对运算数(值和变量)执行操作。例如,
我是一名优秀的程序员,十分优秀!