error: incompatible types in assignment of `std::string' to `char[10]'
解决时间 2021-02-14 20:58
- 提问者网友:人生佛魔见
- 2021-02-14 01:41
#include
#include
using namespace std;
class Student
{
public:
Student(int n,string nam,char s)
{
num=n;
name=nam;
sex=s;
cout<<"Constructor called."< }
~Student()
{
cout<<"Destructor called."< }
void display()
{
cout<<"num:"< cout<<"name:"< cout<<"sex:"< }
private:
int num;
char name[10];
char sex;
};
int main()
{
Student stud1(10010,"wang_li",'f');
stud1.display();
Student stud2(10011,"zhang_fun",'m');
stud2.display();
return 0;
}
【为什么构造函数里,name=nam有错误?请高手帮忙~~】
你觉得你有没有看我的问题啊?你的回答从哪里拷过来的?你要改也得在我的基础上改吧?
最佳答案
- 五星知识达人网友:北方的南先生
- 2021-02-14 02:04
char name[10];
改为
string name;
全部回答
- 1楼网友:底特律间谍
- 2021-02-14 03:16
争议问题比较多,主要的问题有以下几点
1 char separator[]="\t,:\v\r\n\f "; 里面要有' '否则无法区分出单词
2 minlen应该初始化为一个很大的数字,否则永远没有单词的长度小于0的。
minlen = 1000;
3.sentence.assign(sentence,startpos,endpos-startpos);
assign方法会修改字符串sentence的内容,因此sentence直接就变成那个单词了。应该使用下面这行
word=string(sentence,startpos,endpos-startpos);
还有其他一些问题不再赘述。
以下是我修改以后的代码,运行应该是正确的。
我要举报
大家都在看
推荐资讯