免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1259 | 回复: 1
打印 上一主题 下一主题

[C++] 简单的程序,编译不过,怎么改正啊? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-10-03 23:22 |只看该作者 |倒序浏览
本帖最后由 cppfans6 于 2012-10-03 23:24 编辑
  1. #include   <iostream>
  2. #include   <typeinfo>

  3. using   namespace   std;

  4. template   <typename   TP>
  5. struct   COne   
  6. {   
  7.     typedef   TP   one_value_type;
  8. };

  9. template   <typename   COne>
  10. struct   CTwo   
  11. {
  12.         typedef   typename   COne<TP>::one_value_type     two_value_type;   
  13. };


  14. int   main()
  15. {
  16.         typedef COne <int>   OneInt_type;
  17.         typedef CTwo <OneInt_type>   TwoInt_type;
  18.         TwoInt_type::two_value_type   i;
  19.         int   j;
  20.         if   (typeid(i) == typeid(j))      
  21.                 cout << "Right! " <<endl;
  22.         return;
  23. }
复制代码
编译提示错误:

Compiling...
test1.cpp
E:\LAB\stl\test1\test1.cpp(15) : error C2059: syntax error : '<'
        E:\LAB\stl\test1\test1.cpp(16) : see reference to class template instantiation 'CTwo<COne>' being compiled
E:\LAB\stl\test1\test1.cpp(15) : error C2039: 'one_value_type' : is not a member of '`global namespace''
        E:\LAB\stl\test1\test1.cpp(16) : see reference to class template instantiation 'CTwo<COne>' being compiled
E:\LAB\stl\test1\test1.cpp(15) : error C2238: unexpected token(s) preceding ';'
        E:\LAB\stl\test1\test1.cpp(16) : see reference to class template instantiation 'CTwo<COne>' being compiled
E:\LAB\stl\test1\test1.cpp(15) : error C2059: syntax error : '<'
        E:\LAB\stl\test1\test1.cpp(23) : see reference to class template instantiation 'CTwo<struct COne<int> >' being compiled
E:\LAB\stl\test1\test1.cpp(15) : error C2039: 'one_value_type' : is not a member of '`global namespace''
        E:\LAB\stl\test1\test1.cpp(23) : see reference to class template instantiation 'CTwo<struct COne<int> >' being compiled
E:\LAB\stl\test1\test1.cpp(15) : error C2238: unexpected token(s) preceding ';'
        E:\LAB\stl\test1\test1.cpp(23) : see reference to class template instantiation 'CTwo<struct COne<int> >' being compiled
E:\LAB\stl\test1\test1.cpp(23) : error C2039: 'two_value_type' : is not a member of 'CTwo<struct COne<int> >'
E:\LAB\stl\test1\test1.cpp(23) : error C2065: 'two_value_type' : undeclared identifier
E:\LAB\stl\test1\test1.cpp(23) : error C2146: syntax error : missing ';' before identifier 'i'
E:\LAB\stl\test1\test1.cpp(23) : error C2065: 'i' : undeclared identifier
E:\LAB\stl\test1\test1.cpp(27) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.

test1.exe - 10 error(s), 1 warning(s)

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
2 [报告]
发表于 2012-10-03 23:59 |只看该作者
CTwo画蛇添足了.
  1. #include <iostream>
  2. #include <typeinfo>

  3. using namespace std;

  4. template < typename TP >
  5. struct COne {
  6.         typedef TP one_value_type;
  7. };

  8. template < typename TP>
  9. struct CTwo {
  10.         typedef typename TP::one_value_type two_value_type;
  11. };

  12. int main()
  13. {
  14.         typedef COne < int >OneInt_type;
  15.         typedef CTwo < OneInt_type > TwoInt_type;
  16.         TwoInt_type::two_value_type i;
  17.         int j;
  18.         if (typeid(i) == typeid(j))
  19.                 cout << "Right! " << endl;
  20.         return 0;
  21. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP