返回信息流演示代码如下:
struct Student{
int id;
char *name;
int score;
Student(){}
Student(int id_,char *name_,int score_){
id = id_;
name = name_;
score = score_;
}
Student(const Student &s){
this->id = s.id;
this->name = s.name;
this->score = s.score;
}
Student& operator=(const Student &s){
this->id = s.id;
this->name = s.name;
this->score = s.score;
return *this;
}
};
template <int size>
struct object_copy{
template <typename T>
object_copy(const T *source,T *destination){
struct dummy{
T data[size];
};
*reinterpret_cast<dummy*>(destination) = *reinterpret_cast<const dummy*>(source);
}
};
int main(void){
int a[65536],b[65536];
object_copy<65536>(a,b);
Student s[1024],d[1024];
object_copy<1024>(s,d);
return 0;
}
本意是使用模版来实现内存拷贝,但在用Student实例化模版的时候,编译器会在这一句: struct dummy{
T data[size];
};
报错:
fatal error C1001: INTERNAL COMPILER ERROR;
问题:
那么对于这里的int和Student在模版实例化时有神马区别呢?为什么一个能通过一个无法通过?
这是一条镜像帖。来源:北邮人论坛 / cpp / #48454同步于 2010/12/28
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
模版问题
beniao
2010/12/28镜像同步7 回复
订阅后,新回复会通过你的通知中心匿名送达。
7 条回复
【 在 beniao 的大作中提到: 】
: 奇怪,注释掉Student的所有构造函数和重载赋值操作符后,就ok了.难道模版的初始化必须使用编译器产生的默认构造函数?
: --
当然不是,肯定是有地方出问题了。
我没看代码,不知道哪有错,不好意思
模板函数和模板类混淆,改为模板函数
#include <memory.h>
typedef struct Student
{
int id;
char *name;
int score;
Student(){}
Student(int id_,char *name_,int score_)
{
id = id_;
name = name_;
score = score_;
}
Student(const Student &s)
{
this->id = s.id;
this->name = s.name;
this->score = s.score;
}
Student& operator=(const Student &s)
{
this->id = s.id;
this->name = s.name;
this->score = s.score;
return *this;
}
}Student;
template <typename T>
void object_copy(T *destination,const T *source,int size)
{
memcpy(destination,source,size*sizeof(T));
}
int main(void)
{
int a[2],b[2]={1,2};
object_copy<int>(a,b,2);
Student s[2],d[2];
object_copy<Student>(s,d,2);
return 0;
}
囧,你没理解我的想法...
【 在 king2008 的大作中提到: 】
: 模板函数和模板类混淆,改为模板函数
: #include <memory.h>
: typedef struct Student
: ...................
好吧,我在vc6里编译不过,在vs2008里编译过了...
【 在 wo 的大作中提到: 】
: 不太明白楼主的意思,我编译了一下可以运行没问题呀
: --