Error in program. cannot convert from 'char []' to 'char [10]'

G

GRoll35

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!
 
V

Victor Bazarov

GRoll35 said:
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
 
T

TB

GRoll35 sade:
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;
}

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.
 
G

GRoll35

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?
 
V

Victor Bazarov

GRoll35 said:
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

Do you think the types of 'n', 's', 'e', etc. , should be the same or
different from the types of the corresponding arguments in 'Employee'
constructor?
de) : Employee(n, s, e, h)
{
anPay = an;
moPay = mo;
depend = de;
}

anyone have any ideas?

V
 
D

Default User

GRoll35 said:
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 {

There's no point in the dimensions in the parameters. You can't pass
arrays, they are converted to pointers to the first element. So:

Employee(char* n, char* s], char* e, char* h)

would be clearer.
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)

That's pretty obvious, isn't it? n, s, e, and h are of type char, not
char pointers, yet you send them off to the base constructor.
anyone have any ideas?

I would strongly recommend that you stop mucking around with arrays.
Your knowledge is sketchy at this point and arrays are tough nuts prone
to error. For instance, you are using strcpy() without any protection
for array overflow. As I explained above, the dimensions in the
declaration are meaningless.

I would suggest that you use standard containers like std::string at
this stage.



Brian
 
T

Thomas Tutone

GRoll35 said:
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?

I have several ideas, actually. First, compared to many compiler
errors, the above error is remarkably clear and concise: error C2664:
'Employee::Employee(char [],char [],char [],char [])' : cannot convert
parameter 1 from 'char' to 'char []'

In other words, the first parameter is supposed to be a char[] - that
is, an array of characters - but you have passed it a char. An array
of chars is a different type than a char. So which is it?

Second, having a constructor that takes seven arguments is strong
evidence of poor design - the chances of mixing up the order of your
arguments, leading to a very difficult-to-find error, is very high.

Third, as someone else already suggested, you need to stop using
character arrays and start using std::strings, or you are going to
drive yourself nuts. C++ has excellent library facilities. Your life
will be much easier if you use them.

Best regards,

Tom
 
G

GRoll35

do you mean do something like this?

EmployeePay(char n[21], char s[13], char e[6], char h[10], float an,
float mo, int de) : Employee(n[21], s[13], e[6], h[10])
{
anPay = an;
moPay = mo;
depend = de;
}


i guess i'm not getting what you trying to say. i do that and i get 2
more errors. i'll show the errors then the line of code that goes with
them. did i change what you told me to change or am i still missing
something? thank you for your help!

c:\C++\Ch15\Driver.cpp(30): error C2664: 'EmployeePay::EmployeePay(char
[],char [],char [],char [],float,float,int)' : cannot convert parameter
1 from 'char' to 'char []'

EmployeePay(char n[21], char s[13], char e[6], char h[10], float an,
float mo, int de) : Employee(n[21], s[13], e[6], h[10])
{
anPay = an;
moPay = mo;
depend = de;
}

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

EmployeePay myEmp(userName[21], userSSN[13], userNum[6], userHire[10],
userAnPay, userMoPay, userDepends);
 
V

Victor Bazarov

GRoll35 said:
do you mean do something like this?

EmployeePay(char n[21], char s[13], char e[6], char h[10], float an,
float mo, int de) : Employee(n[21], s[13], e[6], h[10])

Close. You've gone too far. Whatever follows the colon is the
initialiser list. It contains, essentially, function calls. When you
call a function, and want to pass an array, you're not supposed to write
the "[21]" (or whatever) after it. If you do, you'll be _dereferencing_
the pointer (indexing the array) and that's something other than what
you're trying to achieve.
{
anPay = an;
moPay = mo;
depend = de;
}


i guess i'm not getting what you trying to say. [...]

Are you going to ask us about every compiler error you're getting? I am
not opposed asking newbie questions, but come on! Make an effort to
understand what your compiler is telling you. How much hand-holding from
the Internet community do you really expect while trying to learn
a programming language? What book are you using to learn C++?

V
 
G

GRoll35

i understand 100% that i need to stay away from them char arrays.
but.. in my assigment this is what it asks for the user input.

Employee Name (i guess I could do string there and have them enter
first name, then last name, then concat them.

Social security number, in the format XXX-XX-XXXX where X is a digit
within the range 0-9

Employee number, in the format XXX-L, where each X is a digit within
the range 0-9, and L is a letter within the range A-M.

Hire Date (01/01/86)

What ways could I do the ssn, emp number, and hire date so its not a
char. if i need it to be formatted like that wouldnt i need it? thanks
guys for putting up with me. I am very new to this and this is all
stuff I need to learn. I appreciate everyone helping.
 
D

Default User

GRoll35 said:
i understand 100% that i need to stay away from them char arrays.
but.. in my assigment this is what it asks for the user input.

Employee Name (i guess I could do string there and have them enter
first name, then last name, then concat them.

Yes, and that's easy to do with std::string.
Social security number, in the format XXX-XX-XXXX where X is a digit
within the range 0-9

That would be a string again, and you need to check the validity of the
individual characters. That's easy.
Employee number, in the format XXX-L, where each X is a digit within
the range 0-9, and L is a letter within the range A-M.

Similar.
Hire Date (01/01/86)

This is trickier, I personally wouldn't want to store the date as a
string, but your requirements may determine that.
What ways could I do the ssn, emp number, and hire date so its not a
char. if i need it to be formatted like that wouldnt i need it? thanks
guys for putting up with me. I am very new to this and this is all
stuff I need to learn. I appreciate everyone helping.

I'm not sure what you are asking. Do you mean for storage or for the
user interface?



Brian
 
V

Victor Bazarov

GRoll35 said:
Employee Name (i guess I could do string there and have them enter
first name, then last name, then concat them.

Social security number, in the format XXX-XX-XXXX where X is a digit
within the range 0-9

'long' and 'unsigned long' have both enough range to represent any SSN as
a number. You will need to play tricks when outputting the number if you
want to see the leading 0s and dashes in the proper places.
Employee number, in the format XXX-L, where each X is a digit within
the range 0-9, and L is a letter within the range A-M.

You could pack it in a 'short' or 'unsigned short' value. You could store
the letter as its (L-'A') value (that's a "minus" in the expression), but
performing those conversions is sometimes a difficult task for a beginner.
Hire Date (01/01/86)

You could convert it into a 'time_t' type using 'mktime' function, but
you're probably better off packing another 'unsigned long' with exact day,
month, and year. Again, packing and unpacking can be daunting, so store
strings for now (until the requirement is to verify the validity of user
input).
What ways could I do the ssn, emp number, and hire date so its not a
char.

See above.
> if i need it to be formatted like that wouldnt i need it?

Sorry, I don't understand the question.
> thanks
guys for putting up with me. I am very new to this and this is all
stuff I need to learn. I appreciate everyone helping.

I think you should take it slowly and go over what you're doing again,
without our help. That way more will settle in your memory.

V
 
S

Shark

char n[21]

O'Reilly sells a book called the C++ Pocket Reference for $9.95. It
says on page 21: "When defining a function that has an array as a
parameter, all but the first dimension must be specified for the
parameter".

In short, instead of char n[21] you have to pass char n[].

Next thing, when you pass an object by value the copy constructor of
that object is called implicitly. In your case a char n[21] doesn't
have a copy constructor (ok, i'm abstracting the abstraction), so it
doesn't fit in with the function calling convention. Blah!

Also, there is deep copy vs. shallow copy which you need to keep in
mind when making copies of arrays (using copy constructors specially)
from your class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top