一个关于c++中的问题(尤其为 指针)
#include"iostream.h"#include"string.h"template<classT,classM>classstudent{private:Tna...
# include "iostream.h"
# include "string.h"
template<class T,class M>
class student
{
private:
T name[10];
M age;
static M length;
public:
student();
void input();
void dele();
void out_top();
void output();
};
template<class T,class M>
student<T,M>::student()
{
name = NULL;
age = 0;
}
template <class T,class M>
M student<T,M>::length = 0;
template<class T,class M>
void student<T,M>::input()
{
M i;
cout<<"Please input the length:"<<endl;
cin>>length;
cout<<"Please input the information:"<<endl;
for (i = 0; i < length; i++)
{
cin>>student[i].name>>student[i].age;
}
}
template<class T,class M>
void student<T,M>::dele()
{
M i;
for (i = 1; i < length; i ++)
{
strcpy(student[i-1].name,student[i].name);
student[i-1].age = student[i].age;
}
length --;
}
template<class T,class M>
void student<T,M>::out_top()
{
cout<<"The top student's information:"<<endl;
cout<<"name:"<<student[0].name<<" "<<"age:"<<student[0].age<<endl;
}
template<class T,class M>
void student<T,M>::output()
{
M i;
for (i = 0; i < length; i ++)
{
cout<<"name:"<<student[i].name<<" "<<"age:"<<student[i].age<<endl;
}
}
void main()
{
student<char,int> *head;
head = new student<char,int>[10];
head->input();
head->out_top();
head->dele();
head->output();
}
请高手帮忙改一下!!!谢谢!!!
不好意思,忘记了:想用类模板来实现:创建一个学生类,先输入 姓名和年龄,后删除栈顶元素 ,输出栈顶元素 ,将所有信息全部输出 ,但在指针上出了问题!!! 展开
# include "string.h"
template<class T,class M>
class student
{
private:
T name[10];
M age;
static M length;
public:
student();
void input();
void dele();
void out_top();
void output();
};
template<class T,class M>
student<T,M>::student()
{
name = NULL;
age = 0;
}
template <class T,class M>
M student<T,M>::length = 0;
template<class T,class M>
void student<T,M>::input()
{
M i;
cout<<"Please input the length:"<<endl;
cin>>length;
cout<<"Please input the information:"<<endl;
for (i = 0; i < length; i++)
{
cin>>student[i].name>>student[i].age;
}
}
template<class T,class M>
void student<T,M>::dele()
{
M i;
for (i = 1; i < length; i ++)
{
strcpy(student[i-1].name,student[i].name);
student[i-1].age = student[i].age;
}
length --;
}
template<class T,class M>
void student<T,M>::out_top()
{
cout<<"The top student's information:"<<endl;
cout<<"name:"<<student[0].name<<" "<<"age:"<<student[0].age<<endl;
}
template<class T,class M>
void student<T,M>::output()
{
M i;
for (i = 0; i < length; i ++)
{
cout<<"name:"<<student[i].name<<" "<<"age:"<<student[i].age<<endl;
}
}
void main()
{
student<char,int> *head;
head = new student<char,int>[10];
head->input();
head->out_top();
head->dele();
head->output();
}
请高手帮忙改一下!!!谢谢!!!
不好意思,忘记了:想用类模板来实现:创建一个学生类,先输入 姓名和年龄,后删除栈顶元素 ,输出栈顶元素 ,将所有信息全部输出 ,但在指针上出了问题!!! 展开
2个回答
展开全部
#include <iostream>
#include <string>
using namespace std;
template<class T,class M>
class student
{
private:
T name[10];
M age;
static M length;
public:
student();
void input();
void dele();
void out_top();
void output();
};
template<class T,class M>
student<T,M>::student()
{
memset(&name,0,sizeof name);
age = 0;
}
template <class T,class M>
M student<T,M>::length = 0;
template<class T,class M>
void student<T,M>::input()
{
M i;
cout<<"Please input the length:"<<endl;
cin>>length;
cout<<"Please input the information:"<<endl;
for (i = 0; i < length; i++)
{
cin >> this[i].name >> this[i].age;
}
}
template<class T,class M>
void student<T,M>::dele()
{
M i;
for (i = 1; i < length; i ++)
{
strcpy(this[i-1].name,this[i].name);
this[i-1].age = this[i].age;
}
length --;
}
template<class T,class M>
void student<T,M>::out_top()
{
cout<<"The top student's information:"<<endl;
cout<<"name:"<<this[0].name<<" "<<"age:"<<this[0].age<<endl;
}
template<class T,class M>
void student<T,M>::output()
{
M i;
for (i = 0; i < length; i ++)
{
cout<<"name:"<<this[i].name<<" "<<"age:"<<this[i].age<<endl;
}
}
int main()
{
student<char,int> *head;
head = new student<char,int>[10];
head->input();
head->out_top();
head->dele();
head->output();
}
我改了下,在我这里是能正常运行的。你试试吧。
你关键是看错了吧,把函数名看成是参数了。估计是从C转来C++的? :-)
#include <string>
using namespace std;
template<class T,class M>
class student
{
private:
T name[10];
M age;
static M length;
public:
student();
void input();
void dele();
void out_top();
void output();
};
template<class T,class M>
student<T,M>::student()
{
memset(&name,0,sizeof name);
age = 0;
}
template <class T,class M>
M student<T,M>::length = 0;
template<class T,class M>
void student<T,M>::input()
{
M i;
cout<<"Please input the length:"<<endl;
cin>>length;
cout<<"Please input the information:"<<endl;
for (i = 0; i < length; i++)
{
cin >> this[i].name >> this[i].age;
}
}
template<class T,class M>
void student<T,M>::dele()
{
M i;
for (i = 1; i < length; i ++)
{
strcpy(this[i-1].name,this[i].name);
this[i-1].age = this[i].age;
}
length --;
}
template<class T,class M>
void student<T,M>::out_top()
{
cout<<"The top student's information:"<<endl;
cout<<"name:"<<this[0].name<<" "<<"age:"<<this[0].age<<endl;
}
template<class T,class M>
void student<T,M>::output()
{
M i;
for (i = 0; i < length; i ++)
{
cout<<"name:"<<this[i].name<<" "<<"age:"<<this[i].age<<endl;
}
}
int main()
{
student<char,int> *head;
head = new student<char,int>[10];
head->input();
head->out_top();
head->dele();
head->output();
}
我改了下,在我这里是能正常运行的。你试试吧。
你关键是看错了吧,把函数名看成是参数了。估计是从C转来C++的? :-)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询