member function definition looks like a ctor, but name does not match enclosing

toadzw 2009-06-10 04:35:25
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include<stdio.h>
#include <vector>
#include <set>
using namespace std;
template <typename T>
class people{
private:
T t;
public:
people(T t1){t=t1;}
print(){cout<<t<<endl;}
};

void main (){
int k=1;
people<int> p(k);
p.print();
}




member function definition looks like a ctor, but name does not match enclosing class


WHAT? WHY? HOW
...全文
677 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lylm 2009-06-10
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 toadzw 的回复:]
可我这本身就只是一个打印的函数,所以才使用VOID啊,难道不对么
[/Quote]

使用void可以
VC6也没问题,输出
1

LZ编译器问题

toadzw 2009-06-10
  • 打赏
  • 举报
回复
可我这本身就只是一个打印的函数,所以才使用VOID啊,难道不对么
night_mare 2009-06-10
  • 打赏
  • 举报
回复
vs2005编译通过
其中print()应该加上一个返回类型
OphoneOu 2009-06-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 taodm 的回复:]
是该汗一下,函数竟然没有申明返回类型。
[/Quote]
member function definition looks like a ctor, but name does not match enclosing class

只有ctor才会没有返回值。
所以你的成员函数print像一个ctor。但是如果这么看print, 那个名字又不对,因为ctor必须跟类同名。
所以上面这两句话的意思。
taodm 2009-06-10
  • 打赏
  • 举报
回复
是该汗一下,函数竟然没有申明返回类型。
zenny_chen 2009-06-10
  • 打赏
  • 举报
回复
呵呵,用people<T>(T t1){t=t1;}试试。
toadzw 2009-06-10
  • 打赏
  • 举报
回复
han!
mengde007 2009-06-10
  • 打赏
  • 举报
回复
管他那么多呢;就是a ctor
老邓 2009-06-10
  • 打赏
  • 举报
回复
VC10编译也通过!
你的编译器是VC6吧,换吧,对模板支持非常不好的。
老邓 2009-06-10
  • 打赏
  • 举报
回复
GCC 4.4.1编译通过!

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <set>
using namespace std;
template <typename T>
class people {
private:
T t;
public:
people(T t1) {t=t1;}
void print() {cout <<t <<endl;}
};

int main() {
int k=1;
people <int> p(k);
p.print();
return 0;
}

64,637

社区成员

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

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