Not being able to pass char array as an argument of a class

Hi everyone,

I have encountered the problem of not being able to pass a char arraray as a parameter into my class, I get the error "prototype for "Employee::Employee(char*,int,char*)' does not match any in class 'Employee' Employee::Employee( char n[20], int a, char j[30]). Thank you in advance for your support.

Header file (employee.h)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

/*No need for passing arguments into the functions since they can
 * call the variables declared in the private access specifier. */

class Employee
{
private:
    char Name[20], Jobtitle[30]; //Why not working ?
    int Age;

public:
    Employee(char, int, char);
    char* getname(); /*Could having the name of the function the same as the variables cause a problem ? Yes it will*/
    int getage();
    char* getjobtitle();
};

#endif // EMPLOYEE_H 



Source file (employee.cpp)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "employee.h"

Employee::Employee( char n[20], int a, char j[30] )
{
  Name = n; Age = a; Jobtitle = j;
}

char* Employee::getname()
{
   return(Name);
}

int Employee::getage()
{
   return(Age);
}

char* Employee::getjobtitle()
{
   return(Jobtitle);
}
Look at your class definition: Employee(char, int, char);
Do you realize that this is defining a constructor that takes a single char, an int, and another single char?

Now look at your constructor implementation: Employee::Employee( char n[20], int a, char j[30] )

Do you realize that this is implementing a constructor that takes a char* an int and another char*?

By the way remember that in a function definition char n[20] is the same as char n[], or char* n.

And lastly, for now, you should be using something like strcpy() to copy those C-strings, or better yet save yourself a bunch of headaches and start using C++ std::string instead of the horrible C-strings.




Last edited on
Hi jlb, thanks for your reply again. But I do not understand my constructor takes a char pointer if I have written Employee::Employee( char n[20], int a, char j[30] ) .

Is there something I am still missing in my constructor ? Or a concept I have mis-understood ?
Is it just because I am using char that I get these issues ?

Very importantly, if I want to pass an array, let's say of type int, is there something else I have to do to not get the same (or other) errors ?

Thanks
Post the complete code and the complete error messages.


By the way remember that in a function definition char n[20] is the same as char n[], or char* n.

Since I cannot post an image or file in here I will post my main.cpp code and give a link to download the print error screen and my full file containing the .pro plus all the other files.

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include "employee.h"
#include "employee.cpp"

using namespace std;

int main()
{
    char nme[20], job[30];
    int years;

    cout << "Hiya! Could you please enter the following info "
         << "for the employee ?" << endl << endl;

    cout << "Name ? ";
    cin >> nme;
    cout << "\n\nAge ? ";
    cin >> years;
    cout << "\n\nJob title ?";
    cin >> job;

    Employee a(nme, years, job);

    cout<< "\n\nThe information you entered is: "
        << a.getname() << ", " << a.getage() << ", " << a.getjobtitle()
        <<"\n\n Thank you for using our services ;)"<<endl;

    return 0;

}


Links to image and file:
https://drive.google.com/drive/folders/1ple7e4K-f7HTJc1d9aUGUDKV6jbb0D0h?usp=sharing
https://drive.google.com/file/d/1s3hNbliCrbEePIX8I5ikJ5Id5NYRdFtL/view?usp=sharing

Where are the modified class files?

Did you change your header file to accept char* instead of single characters?

STOP #including source files! Add the BLOODY .cpp file to your project to be compiled.

Instead of posting hard to read "pictures" just cut and paste the error messages to a post.

Edit: By the way that first link is not pointing to the code in this post.
Last edited on
Topic archived. No new replies allowed.