【发布时间】:2017-08-26 01:30:39
【问题描述】:
#include <iostream>
using namespace std;
template<int x, int y>
void add()
{
cin >> x >> y;
cout << x + y << endl;
}
int main()
{
add<1,2>();
return 0;
}
在Windows10 + Visual Studio 2017中,报错:Binary >> : the operator of the leftoperands of the STD: : istream type is not found(或没有可接受的转换)
参数x 和y 与其他普通的int 变量有什么不同?
【问题讨论】:
-
如果你在没有模板的情况下尝试这个,你会得到同样的错误。为您准备的一些 Google 食物:“运算符优先级”。
-
@SamVarshavchik:您的意思是用常规函数参数替换模板参数?不,在这种情况下不会出错(尽管参数毫无意义,因为函数会立即覆盖它们)。
-
如果您遇到错误,请将错误消息放在您的问题中。
-
x和y不是变量。它们是模板参数。将新值读入常量 1 和 2 意味着什么?