请大家帮看下这个小程序

熊猫过隙 2009-06-11 03:49:35
IDE:VC++ 6.0 (sp6)


#include<iostream>
#include<string>

using namespace std;

class Student
{
public:
Student(int n,string nam,char s) //定义构造函数
{
num = n;
name = nam;
sex = s;

cout << "Constructor called." << endl;
}
~Student() //定义析构函数
{
cout << "Destructor called." << endl;
}
void display()
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl << endl;
}

private:
int num;
char name[10];
char sex;
};

int main()
{
Student stud1(10010,"Wang_li",'f'); //建立对象stud1
stud1.display(); //输出stud1的数据

Student stud2(10011,"Zhang_fun",'m');
stud2.display();

return 0;
}



编译后提示的错误是
--------------------Configuration: test2 - Win32 Debug--------------------
Compiling...
test2.cpp
D:\alexa\VC\test2\test2.cpp(12) : error C2440: '=' : cannot convert from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char [10]'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
执行 cl.exe 时出错.

test2.exe - 1 error(s), 0 warning(s)


不是string重载了赋值操作符了吗,还有出现这种错误?
...全文
102 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
memcpy(name,na,strlen(na)+1 );

这样保证C风格字符串
  • 打赏
  • 举报
回复

class Student
{
public:
Student(int n,string nam,char s) //定义构造函数
{
const char *na;
num = n;
na = nam.c_str(); //用c_str操作
memcpy(name,na,strlen(na) );
sex = s;

cout << "Constructor called." << endl;
}
~Student() //定义析构函数
{
cout << "Destructor called." << endl;
}
void display()
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl << endl;
}

private:
int num;
char name[10];
char sex;
};

int main()
{
Student stud1(10010,"Wang_li",'f'); //建立对象stud1
stud1.display(); //输出stud1的数据

Student stud2(10011,"Zhang_fun",'m');
stud2.display();

return 0;
}
AlwaysSLH 2009-06-11
  • 打赏
  • 举报
回复

name = nam;
改为
strcpy(name,nam.c_str());
老邓 2009-06-11
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
#include <stdio.h>
using namespace std;

class Student
{
public:
Student(int n,string nam,char s) //定义构造函数
{
num = n;
//name = nam;
strcpy(name, nam.c_str());
sex = s;

cout << "Constructor called." << endl;
}
~Student() //定义析构函数
{
cout << "Destructor called." << endl;
}
void display()
{
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl << endl;
}

private:
int num;
char name[10];
char sex;
};

int main()
{
Student stud1(10010,"Wang_li",'f'); //建立对象stud1
stud1.display(); //输出stud1的数据

Student stud2(10011,"Zhang_fun",'m');
stud2.display();

return 0;
}

输出:
Constructor called.
num:10010
name:Wang_li
sex:f

Constructor called.
num:10011
name:Zhang_fun
sex:m

Destructor called.
Destructor called.

Process returned 0 (0x0) execution time : 0.046 s
Press any key to continue.
goodname 2009-06-11
  • 打赏
  • 举报
回复
num = n;
strcpy(name,nam.c_str());//自行确保不要溢出
sex = s;

string的重载了赋值操作符正好和你写的反过来

64,690

社区成员

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

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