问一个模版中的问题

team79 2013-08-16 11:20:54

问一下,为什么上面的注释提示了编译错误,而下面的没有求教
...全文
159 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
team79 2013-08-16
  • 打赏
  • 举报
回复
引用 7 楼 hai200501019 的回复:
[quote=引用 6 楼 hzh_0000 的回复:] [quote=引用 5 楼 hai200501019 的回复:] 不是是注释的错吧。Noncomparable类没有定义 == 操作符,所以才报错吧。下面的是个模板,你没有实例化,没有调用SoftLogic 的compare函数的时候是肯定不会报错的。 class Noncomparable { }; class HardLogic { Noncomparable nc1,nc2; /* void compare() { nc1 == nc2; }*/ }; template <class T> struct SoftLogic { Noncomparable nc1,nc2; void compare() { nc1 == nc2; } }; int main() { SoftLogic<Noncomparable> soft1; soft1.compare(); system("pause"); return 0; } 这样你编译就会发现一样是编译出错
意思是模版的话 SoftLogic<Noncomparable> soft1; 就算有了这个 没有调用它的成员函数,那么成员函数也是没有实例话的么[/quote] 是的,对于模板类的成员函数是这样的,你不调用的成员函数是不会被实例化的。[/quote] 万分感谢
海的神话 2013-08-16
  • 打赏
  • 举报
回复
引用 6 楼 hzh_0000 的回复:
[quote=引用 5 楼 hai200501019 的回复:] 不是是注释的错吧。Noncomparable类没有定义 == 操作符,所以才报错吧。下面的是个模板,你没有实例化,没有调用SoftLogic 的compare函数的时候是肯定不会报错的。 class Noncomparable { }; class HardLogic { Noncomparable nc1,nc2; /* void compare() { nc1 == nc2; }*/ }; template <class T> struct SoftLogic { Noncomparable nc1,nc2; void compare() { nc1 == nc2; } }; int main() { SoftLogic<Noncomparable> soft1; soft1.compare(); system("pause"); return 0; } 这样你编译就会发现一样是编译出错
意思是模版的话 SoftLogic<Noncomparable> soft1; 就算有了这个 没有调用它的成员函数,那么成员函数也是没有实例话的么[/quote] 是的,对于模板类的成员函数是这样的,你不调用的成员函数是不会被实例化的。
team79 2013-08-16
  • 打赏
  • 举报
回复
引用 5 楼 hai200501019 的回复:
不是是注释的错吧。Noncomparable类没有定义 == 操作符,所以才报错吧。下面的是个模板,你没有实例化,没有调用SoftLogic 的compare函数的时候是肯定不会报错的。 class Noncomparable { }; class HardLogic { Noncomparable nc1,nc2; /* void compare() { nc1 == nc2; }*/ }; template <class T> struct SoftLogic { Noncomparable nc1,nc2; void compare() { nc1 == nc2; } }; int main() { SoftLogic<Noncomparable> soft1; soft1.compare(); system("pause"); return 0; } 这样你编译就会发现一样是编译出错
意思是模版的话 SoftLogic<Noncomparable> soft1; 就算有了这个 没有调用它的成员函数,那么成员函数也是没有实例话的么
海的神话 2013-08-16
  • 打赏
  • 举报
回复
不是是注释的错吧。Noncomparable类没有定义 == 操作符,所以才报错吧。下面的是个模板,你没有实例化,没有调用SoftLogic 的compare函数的时候是肯定不会报错的。 class Noncomparable { }; class HardLogic { Noncomparable nc1,nc2; /* void compare() { nc1 == nc2; }*/ }; template <class T> struct SoftLogic { Noncomparable nc1,nc2; void compare() { nc1 == nc2; } }; int main() { SoftLogic<Noncomparable> soft1; soft1.compare(); system("pause"); return 0; } 这样你编译就会发现一样是编译出错
team79 2013-08-16
  • 打赏
  • 举报
回复
引用 1 楼 mougaidong 的回复:
无后效性语句,没有用的。
不懂 无后效性语句 是什么意思。。。。这是C++编程思想上的一题。。。用来理解模版的
team79 2013-08-16
  • 打赏
  • 举报
回复
引用 2 楼 derekrose 的回复:
不懂,是模板起了作用吗?
嗯 加个模版 上面的就不会提示编译错误了
derekrose 2013-08-16
  • 打赏
  • 举报
回复
不懂,是模板起了作用吗?
turing-complete 2013-08-16
  • 打赏
  • 举报
回复
无后效性语句,没有用的。

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧