I am working on a school assignment and have gotten my code down to one error, but I've tried multiple changes and can't figure out how to fix it. I am trying to pass an array from my main() to my class and then have it print out when called.

1 #ifndef EMPLOYEE_H
2 #define EMPLOYEE_H
3 #include <cstring>
4
5 const int DefaultSize = 50;
6
7 class Employee //To define the employee class
8 {
9 private:
10 char EmpName[DefaultSize]; //to hold Employee Name
11 int EmpNum; //to hold Employee Number
12 int HireDate; //to hold date the employee was hired
13
14 public:
15 Employee(); //Constructor
16 void setEmpName (char []);
17 void setEmpNum (int);
18 void setHireDate (int);
19 char getEmpName()const;
20 int getEmpNum() const;
21 int getHireDate() const;
22 };
23
24 Employee::Employee()
25 {
26 std::cout << "Please enter the following employee information: \n";
27 }
28
29 void Employee::setEmpName(char a[])
30 {
31 int index = 0;
32 while (a[index] != '\0')
33 {
34 EmpName[index] = a[index];
35 index++;
36 }
37 EmpName[index] = '\0';
38 }
39
40 void Employee::setEmpNum(int b)
41 {
42 EmpNum = b;
43 }
44
45 void Employee::setHireDate(int c)
46 {
47 HireDate = c;
48 }
49 char Employee::getEmpName() const
50 {
51 return EmpName;
52 }
53 int Employee::getEmpNum() const
54 {
55 return EmpNum;
56 }
57 int Employee::getHireDate() const
58 {
59 return HireDate;
60 }
61
62 #endif

I'm getting the following error:
c:\users\owner\documents\visual studio 2008\projects\project 6\project 6\employee.h(51) : error C2440: 'return' : cannot convert from 'const char [50]' to 'char'

Any help would be very appreciated. I'm still very new to this and I've tried a lot of different ways to try to fix it but keep coming up short.

Use [code] ... [/code] tags -- they will add line numbers for you so that you don't have to do it yourself.

You said the function returns just a single character yet you are trying to return a character array. You can't have it both ways. What you should have done is char* getEmpName()const -- notice there is a * after char to indicate it will return an array of characters.

Edited 6 Years Ago by Ancient Dragon: n/a

I redid the function as follows:

const char *getEmpName()const
	{ 
		return EmpName;
	};

It works great now. Thank you so much.

This article has been dead for over six months. Start a new discussion instead.