cannot convert parameter 2 from 'char [5]' to 'char'
//student.hclassStudent{public:voidset_valve(int,char,char);voiddisplay();private:int...
//student.h
class Student
{
public:
void set_valve(int,char,char);
void display();
private:
int num;
char name;
char sex;
};
//student.cpp
#include<iostream>
#include"student.h"
void Student::display()
{
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
//main.ccp
#include<iostream>
#include"student.h"
int main()
{
void set_valve();
void display();
Student stud;
stud.set_valve(10,"莉莉","女");
stud.display();
return 0;
}
错误:
Compiling...
main.cpp
F:\Microsoft Visual Studio\MyProjects\THX\main.cpp(9) : error C2664: 'set_valve' : cannot convert parameter 2 from 'char [5]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
student.cpp
F:\Microsoft Visual Studio\MyProjects\THX\student.cpp(6) : error C2065: 'cout' : undeclared identifier
F:\Microsoft Visual Studio\MyProjects\THX\student.cpp(6) : error C2297: '<<' : illegal, right operand has type 'char [5]'
F:\Microsoft Visual Studio\MyProjects\THX\student.cpp(6) : error C2065: 'endl' : undeclared identifier
F:\Microsoft Visual Studio\MyProjects\THX\student.cpp(7) : error C2297: '<<' : illegal, right operand has type 'char [6]'
F:\Microsoft Visual Studio\MyProjects\THX\student.cpp(8) : error C2297: '<<' : illegal, right operand has type 'char [5]'
执行 cl.exe 时出错.
求大神帮解答一下啊,看书看到头都要晕了,还没解决这个问题。 展开
class Student
{
public:
void set_valve(int,char,char);
void display();
private:
int num;
char name;
char sex;
};
//student.cpp
#include<iostream>
#include"student.h"
void Student::display()
{
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
//main.ccp
#include<iostream>
#include"student.h"
int main()
{
void set_valve();
void display();
Student stud;
stud.set_valve(10,"莉莉","女");
stud.display();
return 0;
}
错误:
Compiling...
main.cpp
F:\Microsoft Visual Studio\MyProjects\THX\main.cpp(9) : error C2664: 'set_valve' : cannot convert parameter 2 from 'char [5]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
student.cpp
F:\Microsoft Visual Studio\MyProjects\THX\student.cpp(6) : error C2065: 'cout' : undeclared identifier
F:\Microsoft Visual Studio\MyProjects\THX\student.cpp(6) : error C2297: '<<' : illegal, right operand has type 'char [5]'
F:\Microsoft Visual Studio\MyProjects\THX\student.cpp(6) : error C2065: 'endl' : undeclared identifier
F:\Microsoft Visual Studio\MyProjects\THX\student.cpp(7) : error C2297: '<<' : illegal, right operand has type 'char [6]'
F:\Microsoft Visual Studio\MyProjects\THX\student.cpp(8) : error C2297: '<<' : illegal, right operand has type 'char [5]'
执行 cl.exe 时出错.
求大神帮解答一下啊,看书看到头都要晕了,还没解决这个问题。 展开
1个回答
展开全部
/*先分析一下问题,你要存名字,性别,都是一个字符串,你却采用了char这个类型,
char是字符,就是"a","b","<",这一类的字符,所以这里你要把
char name;改为char name[20];,
不一定用20,只是为了确保名字不溢出。性别也一样。
还有,你生命了set_value这个函数,却没有定义,导致调用不了,我修改一下你的代码。
在下面:*/
//student.h
class Student
{
public:
void set_valve(int,char*,char*);
void display();
private:
int num;
char name[20];
char sex[5];
};
//student.cpp
#include<iostream>
#include"student.h"
using namespace std;
void Student::display()
{
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
void Student::set_valve(int _num,char* _name,char* _sex)
{
num = _num;
strcpy_s(name,20,_name);
strcpy_s(sex,5,_sex);
}
//main.ccp
#include<iostream>
#include "student.h"
int main()
{
Student stud;
stud.set_valve(10,"莉莉","女");
stud.display();
return 0;
}
追问
您好,这样我又出现了一个新的错误,是strcpy_s(name,20,_name);
错误是:error C2065: 'strcpy_s' : undeclared identifier。
追答
可以把这个
strcpy_s(name,20,_name);
strcpy_s(sex,5,_sex);
改成
strcpy(name,_name);
strcpy(sex,_sex);
但是这个函数不安全,可能你的编译器不支持strcpy_s函数吧。这个我倒是不清楚了。
祝你学习进步
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |