我有以下代码:错误:“X”不姓模板功能的X型
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
template< typename X>
X unary(X x)
{
return x*10;
}
X binary(X x,X y)
{
return x+y;
}
int main()
{
vector<int> v1{1,2,3,4,5,6};
vector<int> v2(v1);
vector<int>::iterator i;
for(i=v2.begin();i<v2.end();i++)
cout<<*i<<endl;
cout<<"unary "<<unary<int>(2)<<endl;
cout<<"binary "<<binary<int>(2,7);
}
但是,它并不能编译,而是我收到以下错误信息:
transform.cpp:12:1: error: ‘X’ does not name a type
X binary(X x,X y)
transform.cpp: In function ‘int main()’:
transform.cpp:28:19: error: expected primary-expression before template’
以下行出现:
cout<<"binary "<< binary<int,int>(2,7);
为什么X
名称的类型unary
,但不是binary
?
而对于'binary'功能,什么是'X'?该功能不是模板。 –
您需要在本网站上阅读[*本网页以了解正确的询问方式*](https://stackoverflow.com/help/asking)。 – Shadi