程序错误。无法从'char []'转换为'char [10]' [英] Error in program. cannot convert from 'char []' to 'char [10]'

查看:80
本文介绍了程序错误。无法从'char []'转换为'char [10]'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了4个错误。在同一个地方。我会展示我的父母班级,

子班级和我的司机。


所有假设发生的是用户输入数据并使用

显示它的父/子类。


这里是4个错误。


c:\ C ++ \Ch15\Employee.h(29):错误C2440:''='':无法从

''char []''转换为''char [6]''

c:\ C ++ \Ch15\Employee.h(27):错误C2440:''='':无法转换为

''char []''到''char [21]''

c:\ C ++ \Ch15\Employee.h(28):错误C2440:''='':无法转换为

''char []''to''char [13]''

c:\ C ++ \Ch15\Employee.h(30):错误C2440:''='' :无法转换为

''char []''''char [10]''


这是它不是的部分就像我的父班一样。


员工(char n [21], char s [13],char e [6],char h [10])//构造函数

{

name = n;

ssn = s;

empNumber = e;

hire = h;

}


这里是我的父类。


// Employee类的规范文件。

#ifndef EMPLOYEE_H

#define EMPLOYEE_H


班级员工

{

私人:

char name [21];

char ssn [13];

char empNumber [6];

char hire [10];

// char name;

// char ssn;

// char empNumber;

// char hire;

public:

// Employee()//默认控制器

// {

// name ='''';

// ssn ='''';

// empNumber ='''';

// hire ='''';

//}


员工(char n [21],char s [13],char e [6],char h [10])//构造函数

{

name = n;

ssn = s;

empNumbe r = e;

hire = h;

}


~Employee()//析构函数

$

}

char getName()

{

返回名称[21] ;

}

char getSSN()

{

返回ssn [13];

}

char getEmpNumber()

{

return empNumber [6];

}

char getHire()

{

返回租用[10];

}

};

#endif


**这是我的孩子班级


// EmployeePay的规范文件课程

#ifndef员工E_>

#define EMPLOYEEPAY_H

#include" Employee.h"


类EmployeePay:公共员工

{


受保护:

浮动anPay;

浮动moPay ;

int depend;

public:

//派生类构造函数

EmployeePay(char n,char s ,char e,char h,float an,float mo,int

de):员工(n,s,e,h)

{

anPay = an;

moPay = mo;

依赖= de;

}


~EmployeePay()//析构函数

{< br $>
}

浮动getAnPay()

{

返回anPay;

}

浮动getMoPay()

{

返回moPay;

}


int getDepend()

{

返回依赖;

}


};

#endif


**这是我的司机


//这个程序从父母那里获取信息/孩子并在

屏幕上显示

#include< iostream>

#include" EmployeePay.h"

使用命名空间std;


int main()

{

char userName [21],userSSN [13],userNum [6],userHire [10];

float userAnPay,userMoPay;

int userDepends;


// char name [ 21];

// char ssn [13];

// char em pNumber [6];

// char hire [10];


//获取用户的所有信息

cout<<"输入员工的信息:\ n" ;;

cout<<"名称:" ;;

cin。 getline(userName,21);

cout<< 社会安全号码:;

cin.getline(userSSN,13);

cout<< 员工编号:" ;;

cin.getline(userNum,6);

cout<<"雇用日期:" ;;

cin.getline(userHire,10);

cout<< 年薪:;

cin>> userAnPay;

cout<< 每月付款:;

cin>> userMoPay;

cout<< 家属:;

cin>> userDepends;


//定义一个EmployeePay对象并使用用户输入的数据

EmployeePay myEmp(userName [21],userSSN [13],userNum [6],userHire [10],

userAnPay,userMoPay,userDepends);


//显示EmployeePay对象属性

cout<< 这是员工的详细信息:\ n";

cout<< 姓名: << myEmp.getName()<< endl;

cout<< SSN: << myEmp.getSSN()<< endl;

cout<< 员工编号: << myEmp.getEmpNumber()<< endl;

cout<< 雇用日期: << myEmp.getHire()<< endl;

cout<< 年薪: << myEmp.getAnPay()<< endl;

cout<< Montly Pay: << myEmp.getMoPay()<< endl;

cout<< 家属: << myEmp.getDepend()<<结束;


返回0;

}

非常感谢您的帮助!

I get 4 of those errors. in the same spot. I''ll show my parent class,
child class, and my driver.

All that is suppose to happen is the user enters data and it uses
parent/child class to display it.

here is the 4 errors.

c:\C++\Ch15\Employee.h(29): error C2440: ''='' : cannot convert from
''char []'' to ''char [6]''
c:\C++\Ch15\Employee.h(27): error C2440: ''='' : cannot convert from
''char []'' to ''char [21]''
c:\C++\Ch15\Employee.h(28): error C2440: ''='' : cannot convert from
''char []'' to ''char [13]''
c:\C++\Ch15\Employee.h(30): error C2440: ''='' : cannot convert from
''char []'' to ''char [10]''

this is the part that it don''t like in my parent class.

Employee(char n[21], char s[13], char e[6], char h[10]) //constructor
{
name = n;
ssn = s;
empNumber = e;
hire = h;
}

Here is my parent class.

//specification file for the Employee class.
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

class Employee
{
private:
char name[21];
char ssn[13];
char empNumber[6];
char hire[10];
//char name;
//char ssn;
//char empNumber;
//char hire;
public:
//Employee() //default contrusctor
//{
// name ='' '';
// ssn ='' '';
// empNumber='' '';
// hire='' '';
//}

Employee(char n[21], char s[13], char e[6], char h[10]) //constructor
{
name = n;
ssn = s;
empNumber = e;
hire = h;
}

~Employee() //destructor
{
}

char getName()
{
return name[21];
}
char getSSN()
{
return ssn[13];
}
char getEmpNumber()
{
return empNumber[6];
}
char getHire()
{
return hire[10];
}
};
#endif

**here is my child class

//specification file for the EmployeePay class
#ifndef EMPLOYEEPAY_H
#define EMPLOYEEPAY_H
#include "Employee.h"

class EmployeePay: public Employee
{

protected:
float anPay;
float moPay;
int depend;
public:
//derived class contructor
EmployeePay(char n, char s, char e, char h, float an, float mo, int
de) : Employee(n, s, e, h)
{
anPay = an;
moPay = mo;
depend = de;
}

~EmployeePay() //destructor
{
}
float getAnPay()
{
return anPay;
}
float getMoPay()
{
return moPay;
}

int getDepend()
{
return depend;
}

};
#endif

**here is my driver

//this program gets the info from the parent/child and displays on
screen
#include <iostream>
#include "EmployeePay.h"
using namespace std;

int main()
{
char userName[21], userSSN[13], userNum[6], userHire[10];
float userAnPay, userMoPay;
int userDepends;

//char name[21];
//char ssn[13];
//char empNumber[6];
//char hire[10];

//get all the info from the user
cout <<"Enter the employee''s info:\n";
cout <<"Name: ";
cin.getline(userName, 21);
cout << "Social Security Number: ";
cin.getline(userSSN, 13);
cout << "Employee Number: ";
cin.getline(userNum, 6);
cout <<"Hire Date: ";
cin.getline(userHire, 10);
cout << "Annual Pay: ";
cin >> userAnPay;
cout << "Monthly Pay: ";
cin >> userMoPay;
cout << "Dependents: ";
cin >> userDepends;

//define a EmployeePay object and use the data entered by the user
EmployeePay myEmp(userName[21], userSSN[13], userNum[6], userHire[10],
userAnPay, userMoPay, userDepends);

//display the EmployeePay objects properties
cout << "Here are the Employee''s details:\n";
cout << "Name: " << myEmp.getName() << endl;
cout << "SSN: " << myEmp.getSSN() << endl;
cout << "Employee Number: " << myEmp.getEmpNumber() << endl;
cout << "Hire Date: " << myEmp.getHire() << endl;
cout << "Annual Pay: " << myEmp.getAnPay() << endl;
cout << "Montly Pay: " << myEmp.getMoPay() << endl;
cout << "Dependents: " << myEmp.getDepend() << endl;

return 0;
}
thank you very much for any help!

推荐答案

GRoll35写道:
GRoll35 wrote:
我得到4个错误。在同一个地方。我会展示我的父母班,孩子班和我的司机。

所有假设发生的是用户输入数据并使用
父/子班显示它。

这是4个错误。

c:\ C ++ \Ch15\Employee.h(29):错误C2440:''=' ':无法从
''char []''转换为''char [6]''
c:\ C ++ \Ch15 \ Employer.h(27):错误C2440:' '='':无法从
''char []''转换为''char [21]''
c:\ C ++ \Ch15\Employee.h(28):错误C2440:''='':无法从
''char []''转换为''char [13]''
c:\ C ++ \Ch15\Employee.h(30 ):错误C2440:''='':无法从
''char []''转换为''char [10]''

这是它不应该的部分不喜欢我的父班。

员工(char n [21],char s [13],char e [6],char h [10])//构造函数
{
name = N;


数组不可分配。请使用''std :: memcpy''或''std :: strcpy'',或

''std :: copy''。

[..]
}

这是我的父类。

// Employee类的规范文件。
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

班级员工
{
私人:
char name [21];
[..]
I get 4 of those errors. in the same spot. I''ll show my parent class,
child class, and my driver.

All that is suppose to happen is the user enters data and it uses
parent/child class to display it.

here is the 4 errors.

c:\C++\Ch15\Employee.h(29): error C2440: ''='' : cannot convert from
''char []'' to ''char [6]''
c:\C++\Ch15\Employee.h(27): error C2440: ''='' : cannot convert from
''char []'' to ''char [21]''
c:\C++\Ch15\Employee.h(28): error C2440: ''='' : cannot convert from
''char []'' to ''char [13]''
c:\C++\Ch15\Employee.h(30): error C2440: ''='' : cannot convert from
''char []'' to ''char [10]''

this is the part that it don''t like in my parent class.

Employee(char n[21], char s[13], char e[6], char h[10]) //constructor
{
name = n;
Arrays are not assignable. Please use ''std::memcpy'' or ''std::strcpy'', or
''std::copy''.
[..]
}

Here is my parent class.

//specification file for the Employee class.
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

class Employee
{
private:
char name[21];
[..]



V



V


GRoll35 sade:
GRoll35 sade:
我得到4个错误。在同一个地方。我会展示我的父母班,孩子班和我的司机。

所有假设发生的是用户输入数据并使用
父/子班显示它。

这是4个错误。

c:\ C ++ \Ch15\Employee.h(29):错误C2440:''=' ':无法从
''char []''转换为''char [6]''
c:\ C ++ \Ch15 \ Employer.h(27):错误C2440:' '='':无法从
''char []''转换为''char [21]''
c:\ C ++ \Ch15\Employee.h(28):错误C2440:''='':无法从
''char []''转换为''char [13]''
c:\ C ++ \Ch15\Employee.h(30 ):错误C2440:''='':无法从
''char []''转换为''char [10]''

这是它不应该的部分不喜欢我的父班。

员工(char n [21],char s [13],char e [6],char h [10])//构造函数
{
name = n;
ssn = s;
empNumber = e;
雇用= h;
}
I get 4 of those errors. in the same spot. I''ll show my parent class,
child class, and my driver.

All that is suppose to happen is the user enters data and it uses
parent/child class to display it.

here is the 4 errors.

c:\C++\Ch15\Employee.h(29): error C2440: ''='' : cannot convert from
''char []'' to ''char [6]''
c:\C++\Ch15\Employee.h(27): error C2440: ''='' : cannot convert from
''char []'' to ''char [21]''
c:\C++\Ch15\Employee.h(28): error C2440: ''='' : cannot convert from
''char []'' to ''char [13]''
c:\C++\Ch15\Employee.h(30): error C2440: ''='' : cannot convert from
''char []'' to ''char [10]''

this is the part that it don''t like in my parent class.

Employee(char n[21], char s[13], char e[6], char h[10]) //constructor
{
name = n;
ssn = s;
empNumber = e;
hire = h;
}




除非你' '是一个速度怪,考虑使用std :: string,

这样你就不会歧视名字长于

21个字符的人;)


O,顺便说一句,你不能用这种方式为彼此分配数组,

你必须将源数组复制到目标数组。


-

TB @ SWEDEN



Unless you''re a speed-freak, consider using std::string instead,
that way you won''t discriminate people with names longer than
21 characters ;)

O, by the way, you can''t assign arrays to each other that way,
you have to copy source array to destination array.

--
TB @ SWEDEN


确定解决了这个问题。所以现在它看起来像这样..


员工(char n [21],char s [13],char e [6],char h [10])//构造函数

{

strcpy(name,n);

strcpy(ssn,s);

strcpy(empNumber, e);

strcpy(租用,h);

}

但现在我收到此错误...


c:\ C ++ \Ch15\EmployeePay.h(16):错误C2664:''员工::员工(char

[],char [],char [ ],char [])'':无法将参数1从''char''

转换为''char []''


并且它指向这部分代码..


EmployeePay(char n,char s,char e,char h,float an,float mo,int

de ):员工(n,s,e,h)

{

anPay = an;

moPay = mo;

依赖= de;

}


任何人都有任何想法吗?

ok that fixed that problem. so now it looks like this..

Employee(char n[21], char s[13], char e[6], char h[10]) //constructor
{
strcpy(name,n);
strcpy(ssn,s);
strcpy(empNumber,e);
strcpy(hire,h);
}
but now i get this error...

c:\C++\Ch15\EmployeePay.h(16): error C2664: ''Employee::Employee(char
[],char [],char [],char [])'' : cannot convert parameter 1 from ''char''
to ''char []''

and it points to this part of the code..

EmployeePay(char n, char s, char e, char h, float an, float mo, int
de) : Employee(n, s, e, h)
{
anPay = an;
moPay = mo;
depend = de;
}

anyone have any ideas?


这篇关于程序错误。无法从'char []'转换为'char [10]'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆