weixin_42102531 2018-04-29 09:37 采纳率: 0%
浏览 1232
已结题

类模板的成员函数怎么特化,急求帮助,编译器为dev,才学的萌新急求大神教

#include"Shape.h"
#include"Point.h"
#include"Rect.h"
#include"Line.h"
#include"Line.cpp"
#include
#include
using namespace std;
template//我的模板写在主函数里了
class List
{
private:
T* l;
int size;
public:
List(){size=0,l=NULL;}
List(int Len){l=new T[Len],size=Len;}
~List(){if(l!=NULL) delete[]l;}
void Setsize(int s){size=s;}
void GetMassage();//我希望针对不同的文件读取,特化这个函数
};
template
void List::GetMassage()
{
fstream f;
//if(typeid(T)==typeid(Line))//
if(choice==2)
{
int i=0;
f.open("Line.txt",ios::in);
while(1)
{
float temp=0;
//char s[20];
f>>temp;
l[i].Shape::Putid(temp);
cout< f>>temp;
l[i].Setp1x(temp);
cout< f>>temp;
l[i].Setp1y(temp);
cout< f>>temp;
l[i].Setp2x(temp);
cout< f>>temp;
l[i].Setp2y(temp);
cout< f>>temp;
l[i].Setlen(temp);
cout< if(f.peek()==EOF) break;
i++;
}
i--;
Setsize(i);
f.close();
}
else if(choice==1) //if(typeid(T)==typeid(Point))
{
int i=0;
f.open("Point.txt",ios::in);
while(1)
{
float temp=0;
char s[20];
f>>temp;
l[i].Shape::Putid(temp);
cout< f>>temp;
l[i].Setx(temp);
cout< f>>temp;
l[i].Sety(temp);
cout< f>>s;
l[i].Shape::Putdes((string)s);
if(f.peek()==EOF) break;//�������ǻ�����һ�Σ�����������������Ҫ��ȥ
i++;
}
i--;
Setsize(i);
f.close();
}
}
int main()
{
List point(12000);//����Ϊ���ޣ���size��ͨ���ļ���ȡ��������
List rect(12000);
List line(12000);
Line t(1,2,3,4,5);
cout<<t;
point.GetMassage();
line.GetMassage();

}
//Shape.h
#ifndef SHAPE_H
#define SHAPE_H
#include
#include
#include
using namespace std;
class Shape
{
private:
int obj_id;//������ʶ��
string des;//������������
public:
virtual float Area()=0;//���麯��
virtual void WriteToFile(ofstream& o)=0;//���麯��
int Getid(){return obj_id;}
string Getdes(){return des;}
void Putid(int i){obj_id=i;}
void Putdes(string s){des=s;}
};
#endif
//Point.h
#ifndef POINT_H
#define POINT_H
#include"Shape.h"
class Point:public Shape//������
{
private:
float x,y;
static int count;
public:
Point(float xx=0,float yy=0):x(xx),y(yy){}
Point(Point& copy){x=copy.Getx(),y=copy.Gety();}
float Getx(){return x;}
float Gety(){return y;}
void Setx(float xx){x=xx;}//��ֵ����
void Sety(float yy){y=yy;}
virtual float Area(){return 0;}//
virtual void WriteToFile(ofstream& o){cout<<"aa";}//
static int GetCount(){return count;}
friend ostream&operator<<(ostream &o,Point &obj);
};
#endif
//这个可以忽略,下面是运算符重载
#include"Point.h"
#include
#include
using namespace std;
ostream &operator<<(ostream &o,Point &obj)//Point����������
{
o<<"("< return o;
}
//下面是line的相关代码
//Line.h
#ifndef LINE_H
#define LINE_H
#include"Shape.h"
#include"Point.h"
class Line:public Shape
{
private:
Point p1;
Point p2;
double len;
static int count;
public:
Line():p1(0,0),p2(0,0),len(0){}
Line(float xx1,float yy1,float xx2,float yy2,double length):p1(xx1,yy1),p2(xx2,yy2),len(length){}
virtual float Area(){return 0;}//
virtual void WriteToFile(ofstream& o){cout float Getp1x(){return p1.Getx();}
float Getp2x(){return p2.Getx();}
float Getp1y(){return p1.Gety();}
float Getp2y(){return p2.Gety();}
void Setp1x(float a){p1.Setx(a);}
void Setp1y(float a){p1.Sety(a);}
void Setp2x(float a){p2.Setx(a);}
void Setp2y(float a){p2.Sety(a);}
void Setlen(double a){len=a;}
double Getlen(){return len;}
friend ostream &operator };
#endif
//这个也是运算符重载,可忽略
#include"Line.h"
#include
#include
using namespace std;
ostream &operator<<(ostream &o,Line &obj)//Line����������
{
o<<"("<<obj.Getp1x()<<","<<obj.Getp1y()<<")"<<endl
<<"("<<obj.Getp2x()<<","<<obj.Getp2y()<<")"<<endl
<<obj.Getlen()<<endl;
return o;
}
//Rect和问题无关,在这我就不给出来了

  • 写回答

1条回答 默认 最新

  • james8172 2018-04-29 10:20
    关注

    Rect和问题无关,在这我就不给出来了

    评论

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python