这个程序怎么调啊。谢谢

熊猫过隙 2008-10-08 02:54:02
#include<iostream>
#include<string>

using namespace std;

class Teacher
{
public:
Teacher(string nam,int a,string t)
{
name = nam;
age = a;
title = t;
}
void display()
{
cout << "name:" << name << endl;
cout << "age:" << age <<endl;
cout << "title:" << title <<endl;
}

protected:
string name;
int age;
string title;
};

class Student
{
public:
Student(string nam,char s,float sco)
{
strcpy(name1,nam);
sex = s;
score = sco;
}
void display1()
{
cout << "name:" << name1 << endl;
cout << "sex:" << sex <<endl;
cout << "score:" << score <<endl;
}

protected:
string name1;
char sex;
float score;
};

class Graduate:public Teacher,public Student
{
public:
Graduate(string nam,int a,char s,string t,float sco,float w)
:Teacher(nam,a,t),Student(nam,s,sco),wage(w){}
void show()
{
cout << "name:" << name << endl;
cout << "age:" << age << endl;
cout << "sex:" << sex << endl;
cout << "score:" << score << endl;
cout << "title:" << title << endl;
cout << "wages:" << wage <<endl;
}

private:
float wage;
};

int main()
{

Graduate grad1("Wangli",24,'f',"assistant",89.5,1234.5);
grad1.show();

return 0;
}



运行后出错的提示如下(在VS2005中运行)
1>正在编译...
1>main.cpp
1>c:\documents and settings\panda\my documents\visual studio 2005\projects\blank1\blank1\main.cpp(33) : error C2664: “strcpy”: 不能将参数 1 从“std::string”转换为“char *”
1> 没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符
1>生成日志保存在“file://c:\Documents and Settings\Panda\My Documents\Visual Studio 2005\Projects\blank1\blank1\Debug\BuildLog.htm”
1>blank1 - 1 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========


...全文
81 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
熊猫过隙 2008-10-08
  • 打赏
  • 举报
回复
谢谢2楼
littlechen 2008-10-08
  • 打赏
  • 举报
回复
楼主真有钱
OenAuth.Core 2008-10-08
  • 打赏
  • 举报
回复
只有char数组必须用strcpy,string是个类,它重载了赋值操作符
OenAuth.Core 2008-10-08
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>

using namespace std;

class Teacher
{
public:
Teacher(string nam,int a,string t)
{
name = nam;
age = a;
title = t;
}
void display()
{
cout << "name:" << name << endl;
cout << "age:" << age <<endl;
cout << "title:" << title <<endl;
}

protected:
string name;
int age;
string title;
};

class Student
{
public:
Student(string nam,char s,float sco)
{
name1=nam; //直接=就可以了,string支持=
sex = s;
score = sco;
}
void display1()
{
cout << "name:" << name1 << endl;
cout << "sex:" << sex <<endl;
cout << "score:" << score <<endl;
}

protected:
string name1;
char sex;
float score;
};

class Graduate:public Teacher,public Student
{
public:
Graduate(string nam,int a,char s,string t,float sco,float w)
:Teacher(nam,a,t),Student(nam,s,sco),wage(w){}
void show()
{
cout << "name:" << name << endl;
cout << "age:" << age << endl;
cout << "sex:" << sex << endl;
cout << "score:" << score << endl;
cout << "title:" << title << endl;
cout << "wages:" << wage <<endl;
}

private:
float wage;
};

int main()
{

Graduate grad1("Wangli",24,'f',"assistant",89.5,1234.5);
grad1.show();

return 0;
}

64,682

社区成员

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

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