免费注册 查看新帖 |

Chinaunix

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

非模板类,可以有模板函数么? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-06-10 12:55 |只看该作者 |倒序浏览
10可用积分
本帖最后由 songweibo 于 2012-06-10 12:56 编辑

我用VC8编译下面的代码提示错误:

  1. #include "stdafx.h"

  2. class my{
  3. public:
  4.     my():i(2),j(3){printf("ctor\n");}
  5.     ~my(){printf("dtor\n");}

  6.         template<class T>
  7.         vod add( T t ){
  8.                 i+=t;
  9.         }
  10. private
  11.     int i,j;
  12. };
  13. int main(int argc,char**){
  14.         my m1;
  15.         m1.add<short>(3);
  16.     return 0;
  17. }
复制代码
提示我有一堆错误:
1>my.cpp(9): error C2146: syntax error : missing ';' before identifier 'add'
1>my.cpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>my.cpp(9): error C2238: unexpected token(s) preceding ';'
1>my.cpp(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>my.cpp(11): error C2143: syntax error : missing ';' before '<end Parse>'
1>my.cpp(11): warning C4183: 'add': missing return type; assumed to be a member function returning 'int'
1>my.cpp(13): error C2238: unexpected token(s) preceding ';'
1>my.cpp(5): error C2614: 'my' : illegal member initialization: 'j' is not a base or member
1>my.cpp(5): error C2614: 'my' : illegal member initialization: 'i' is not a base or member
1>my.cpp(17): error C2065: 'm1' : undeclared identifier
1>my.cpp(17): error C2062: type 'short' unexpected

如果我在class 前面加上模板声明
template<class U>
使用的时候变成        my<int> m1;
这次错误更多了

1>my.cpp(9): error C2146: syntax error : missing ';' before identifier 'add'
1>          my.cpp(14) : see reference to class template instantiation 'my<U>' being compiled
1>my.cpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>my.cpp(9): error C2238: unexpected token(s) preceding ';'
1>my.cpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>my.cpp(9): error C2143: syntax error : missing ';' before '<end Parse>'
1>my.cpp(9): warning C4183: 'add': missing return type; assumed to be a member function returning 'int'
1>my.cpp(13): error C2238: unexpected token(s) preceding ';'
1>my.cpp(9): error C2146: syntax error : missing ';' before identifier 'add'
1>          my.cpp(16) : see reference to class template instantiation 'my<U>' being compiled
1>          with
1>          [
1>              U=int
1>          ]
1>my.cpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>my.cpp(9): error C2238: unexpected token(s) preceding ';'
1>my.cpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>my.cpp(9): error C2143: syntax error : missing ';' before '<end Parse>'
1>my.cpp(9): warning C4183: 'add': missing return type; assumed to be a member function returning 'int'
1>my.cpp(13): error C2238: unexpected token(s) preceding ';'

到底错在哪里呢?

最佳答案

查看完整内容

#include "stdafx.h"class my{public: my():i(2),j(3){printf("ctor\n");} ~my(){printf("dtor\n");} template void add( T t ){ i+=t; }private: int i,j;};int main(int argc,char** argv){ my m1; m1.add(3); return 0;}

论坛徽章:
0
2 [报告]
发表于 2012-06-10 12:55 |只看该作者
本帖最后由 Moon_Bird 于 2012-06-10 15:13 编辑

#include "stdafx.h"

class my{
public:
    my():i(2),j(3){printf("ctor\n");}
    ~my(){printf("dtor\n");}

        template<class T>
        void add( T t ){
                i+=t;
        }
private:
    int i,j;
};
int main(int argc,char** argv){
        my m1;
        m1.add<short>(3);
    return 0;
}

论坛徽章:
0
3 [报告]
发表于 2012-06-10 13:10 |只看该作者
你换一个支持语法高亮的编辑器吧。
你把 void 拼错了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP