有关模板问题

luozike_110110 2009-05-08 07:41:15
在class_ex.h中
#include<iostream>
using namespace std;
class Time
{
private:
int me;
public:
int hour;
int minute;
int second;

static int number;

Time(int h,int m,int s);
void getTime();
friend void display(Time &t);
};
template <class numtype1,class numtype2>
class Cl
{
public:
numtype1 a;
numtype2 b;
Cl(numtype1 n,numtype2 m);
// {
// a=n;b=m;
// }
void display1();
// {
// cout<<a<<endl;
// cout<<b<<endl;
// }
};

//关于继承中强制类型的转换
class A
{
private:
int a;
public:
A(int a)
{
this->a=a;
}
virtual ~A()
{
cout<<"this is A del"<<endl;
}
};

class B:public A
{
private :
int b;
public:
B(int a,int b):A(a)
{
this->b=b;
}
~B() //将之改为虚析构函数结果将怎样
{
cout<<"this is B del"<<endl;
}
};

class C:public B
{
private:
int c;
public:
C(int a,int b,int c):B(a,b)
{
this->c=c;
}
~C()
{
cout<<"this is C del"<<endl;
}
};


在实现CPP中
#include<iostream>
#include"class_point.h"
using namespace std;

void Time::getTime()
{
cout<<hour<<" :"<<minute<<" :"<<second<<endl;
cout<<number<<endl;
}
Time::Time(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
me=55;
}
int Time::number=10;
void display(Time &t)
{
cout<<t.me<<endl;
}

//这里有问题

template <class numtype1,class numtype2>
void Cl<numtype1,numtype2>::display1()
{
cout<<"a "<<a<<endl;
cout<<"b "<<b<<endl;
}
template <class numtype1,class numtype2>
Cl<numtype1,numtype2>::Cl(numtype1 n,numtype2 m)
{
a=n;b=m;
}


main的cpp文件中

#include<iostream>
#include"class_point.h"
using namespace std;

void main()
{
Cl<int,int> c1(12,15);
c1.display1();

}


运行时出问题了!!请教各位
...全文
99 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
luozike_110110 2009-05-30
  • 打赏
  • 举报
回复
thank all of your!
pathuang68 2009-05-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hairetz 的回复:]
又是这个问题。
模板函数的声明跟定义都放到.h里,不要分开。否则考虑用export关键字,支持模板的分开编译。
[/Quote]
对。
surlover 2009-05-14
  • 打赏
  • 举报
回复
不支持分离编译,声明和定义都放到头文件中

至少VS2008还不支持export关键字
Jalien 2009-05-14
  • 打赏
  • 举报
回复
可以使用包含编译的 而且并不是所有编译器都实现了分别编译,但所有的编译器都实现了包含编译[Quote=引用 4 楼 hairetz 的回复:]
又是这个问题。
模板函数的声明跟定义都放到.h里,不要分开。否则考虑用export关键字,支持模板的分开编译。
[/Quote]
  • 打赏
  • 举报
回复
又是这个问题。
模板函数的声明跟定义都放到.h里,不要分开。否则考虑用export关键字,支持模板的分开编译。
mengde007 2009-05-08
  • 打赏
  • 举报
回复
VS2005下面木有问题;
mengde007 2009-05-08
  • 打赏
  • 举报
回复


#include <string>
#include <iostream>

using namespace std;


class Time
{
private:
int me;
public:
int hour;
int minute;
int second;

static int number;

Time(int h,int m,int s);
void getTime();
friend void display(Time &t);
};

template <class numtype1,class numtype2>
class Cl
{
public:
numtype1 a;
numtype2 b;
Cl(numtype1 n,numtype2 m) ;

void display1() ;

};

//关于继承中强制类型的转换
class A
{
private:
int a;
public:
A(int a)
{
this->a=a;
}
virtual ~A()
{
cout <<"this is A del" <<endl;
}
};

class B:public A
{
private :
int b;
public:
B(int a,int b):A(a)
{
this->b=b;
}

virtual ~B() //将之改为虚析构函数结果将怎样
{
cout <<"this is B del" <<endl;
}
};

class C:public B
{
private:
int c;
public:
C(int a,int b,int c):B(a,b)
{
this->c=c;
}
~C()
{
cout <<"this is C del" <<endl;
}
};



void Time::getTime()
{
cout <<hour <<" :" <<minute <<" :" <<second <<endl;
cout <<number <<endl;
}
Time::Time(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
me=55;
}
int Time::number=10;
void display(Time &t)
{
cout <<t.me <<endl;
}

//这里有问题

template <class numtype1,class numtype2>
void Cl <numtype1,numtype2>::display1()
{
cout <<"a " <<a <<endl;
cout <<"b " <<b <<endl;
}

template <class numtype1,class numtype2>
Cl <numtype1,numtype2>::Cl(numtype1 n,numtype2 m)
{
a=n;b=m;
}



void main()
{
Cl <int,int> c1(12,15);
c1.display1();

}

baihacker 2009-05-08
  • 打赏
  • 举报
回复

单文件没有出现问题

d:\Projects>g++ c.c
c.c:129:3: warning: no newline at end of file

d:\Projects>a
a 12
b 15

你把模板的实现分开了...一般不支持这样搞的...

#include <iostream>
using namespace std;
class Time
{
private:
int me;
public:
int hour;
int minute;
int second;

static int number;

Time(int h,int m,int s);
void getTime();
friend void display(Time &t);
};
template <class numtype1,class numtype2>
class Cl
{
public:
numtype1 a;
numtype2 b;
Cl(numtype1 n,numtype2 m);
// {
// a=n;b=m;
// }
void display1();
// {
// cout <<a <<endl;
// cout <<b <<endl;
// }
};

//关于继承中强制类型的转换
class A
{
private:
int a;
public:
A(int a)
{
this->a=a;
}
virtual ~A()
{
cout <<"this is A del" <<endl;
}
};

class B:public A
{
private :
int b;
public:
B(int a,int b):A(a)
{
this->b=b;
}
~B() //将之改为虚析构函数结果将怎样
{
cout <<"this is B del" <<endl;
}
};

class C:public B
{
private:
int c;
public:
C(int a,int b,int c):B(a,b)
{
this->c=c;
}
~C()
{
cout <<"this is C del" <<endl;
}
};


//在实现CPP中
#include <iostream>
//#include"class_point.h"
using namespace std;

void Time::getTime()
{
cout <<hour <<" :" <<minute <<" :" <<second <<endl;
cout <<number <<endl;
}
Time::Time(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
me=55;
}
int Time::number=10;
void display(Time &t)
{
cout <<t.me <<endl;
}

//这里有问题

template <class numtype1,class numtype2>
void Cl <numtype1,numtype2>::display1()
{
cout <<"a " <<a <<endl;
cout <<"b " <<b <<endl;
}
template <class numtype1,class numtype2>
Cl <numtype1,numtype2>::Cl(numtype1 n,numtype2 m)
{
a=n;b=m;
}


#include <iostream>
//#include"class_point.h"
using namespace std;

int main()
{
Cl <int,int> c1(12,15);
c1.display1();

}

65,211

社区成员

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

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