关于template中operator的问题

skineffect skineffect 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include<iostream>
using namespace std;
template<class T> struct plus{
T operator()(const T& x,const T& y)const{
return x+y;
}
};
template<class T> struct minus{
T operator()(const T& x,const T& y)const{
return x-y;
}
};
int main(){
plus<int> plusobj; //在编译时说plus没定义。
minus<int> minusobj;
cout<<plusobj(3,5)<<endl;
cout<<minusobj(3,5)<<endl;

cout<<plus<int>()(43,50)<<endl;//这两句的语法比较诡异,是将plus<int>()当成一个函数名了吗?
cout<<minus<int>()(43,50)<<endl;
}

rt,上面的程序在devC++中编译出问题如下:
14 E:\skin\学习\C++相关\stl源码剖析\opertor多载化.cpp `plus' undeclared (first use this function) 
14 E:\skin\学习\C++相关\stl源码剖析\opertor多载化.cpp expected primary-expression before "int"
。。。。。。。。。。。。。。

最近被template搞的很崩溃,想学学stl所以先看了看template。应该怎样学stl最好呢,大牛们给给建议吧。
...全文
评论 11
收藏 已收藏
分享
返回顶部
skineffect
C++ 语言 65,214 成员 250,514 内容