Employee Class Problem #2

Hey guys, so I'm trying to input 3 different data for a class array. However I keep getting these errors...

error C2146: syntax error : missing ';' before identifier 'objects'
warning C4551: function call missing argument list
error C2065: 'objects' : undeclared identifier
error C2660: 'Employee' : function does not take 4 arguments

"objects" is in the main file, other than that I can't get it to compile...
thanks in advance!!!!

Header File:
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Employee Class Program
//Program by: Andrew A

#ifndef Employee_Header
#include <sstream>
#include <iostream>
#include <string>

using namespace std;

class Employee
{
private:
	string name;
	string idnumber;
	string department;
	string position;
public:
	Employee ();
	Employee (string n, string id);
	Employee (string n, string id, string dept, string pos);

	string Employee::getNAME()
	{
		return name;
	}

	string Employee::getID()
	{
		return idnumber;
	}

	string Employee::getDEPT()
	{
		return department;
	}

	string Employee::getPOS()
	{
		return position;
	}
};

void Employee()
{
	string name=" ";
	string idnumber = 0;
	string department = " ";
	string position = " ";



};

#endif 


Main File:
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
31
32
33
//Employee Class Program
//By: Andrew A.

#include "EmployeeClass.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
	const int number = 3;

	Employee objects[number] =
	{
		Employee ("Susan Meyers", "47899", "Accounting", "Vice President"),
		Employee ("Mark Jones", "39119", "IT", "Programmer"),
		Employee ("Roy Rogers", "81774", "Manufacturing", "Engineer")
	};

	for(int i = 0; i < number; i++)
    {
        cout << objects[i].getNAME() << " ";
        cout << objects[i].getID() << " ";
        cout << objects[i].getDEPT() << " ";
	cout << objects[i].getPOS() << endl;
    };


	cin.ignore();
	cin.get();
	return 0;
}
You didn't implement the constructor that takes the four arguments but rather you declared it.

In C++, when you say something like this:

Employee (string n, string id, string dept, string pos);

You are simply telling the compiler that you are gonna use a function like that but without a body like you have the default constructor (which by the way is missing the scope resolution operator and should therefore look like this) you can't call the function:

1
2
3
4
5
6
7
Employee::Employee()
{
    string name = " ";
    string idnumber = "0";
    string department = " ";
    string position = " ";
}



All in all, you need to have a function definition for the following function:

1
2
3
4
Employee::Employee(string n, string id, string dept, string pos)
{
    //fill in the code here
}
Last edited on
Why are you using

Employee::

in side class

you should define function like this in class{}
1
2
3
4
string getNAME()
	{
		return name;
	}

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Employee
{
private:
        string name;
        string idnumber;
        string department;
        string position;
public:
        Employee ();
        Employee (string n, string id); //not defined
        Employee (string n, string id, string dept, string pos);

        string getNAME()
        {
                return name;
        }

        string getID()
        {
                return idnumber;
        }

        string getDEPT()
        {
                return department;
        }

        string getPOS()
        {
                return position;
        }
};

Employee::Employee()
{
        name=" ";
        idnumber = "";
        department = " ";
        position = " ";

}

Employee::Employee (string n, string id, string dept, string pos)
{
    name=n;
    idnumber = id;
    department = dept;
    position = pos;
}



int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    const int number = 3;

    Employee *objects[number];


        objects[0] = new Employee("Susan Meyers", "47899", "Accounting", "Vice President");
        objects[1] = new Employee("Mark Jones", "39119", "IT", "Programmer");
        objects[2] = new Employee("Roy Rogers", "81774", "Manufacturing", "Engineer");



    for(int i = 0; i < number; i++)
{
    cout << objects[i]->getNAME() << " ";
    cout << objects[i]->getID() << " ";
    cout << objects[i]->getDEPT() << " ";
    cout << objects[i]->getPOS() << endl;
};



    cin.ignore();
    cin.get();
	return 0;
}
thanks "a null byte" I see what you're saying. "ak16" definitely spelled it out for me lol. I see what you mean by defining the function.

however when I added this, these things got underlined in red. I looked into this code online and people say its for a different compiler/application whatever. "object" is still underlined in red
1
2
3
4
5
6
7
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    const int number = 3;

   Employee *objects[number];


What error message is the compiler giving you?
its giving me this...
"error C2065: 'QApplication' : undeclared identifier"
"error C3861: 'a': identifier not found"
"error C2065: 'MainWindow' : undeclared identifier"
"error C2065: 'w' : undeclared identifier"
"error C2228: left of '.show' must have class/struct/union"
"error C2065: 'objects' : undeclared identifier"

Here is my current code...
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//Employee Class Program
//Program by: Andrew A

#ifndef Employee_Header
#include <sstream>
#include <iostream>
#include <string>

using namespace std;

class Employee
{
private:
	string name;
	string idnumber;
	string department;
	string position;
public:
	Employee ();
	Employee (string n, string id, string dept, string pos);

	string getNAME()
	{
		return name;
	}

	string getID()
	{
		return idnumber;
	}

	string getDEPT()
	{
		return department;
	}

	string getPOS()
	{
		return position;
	}

};

Employee::Employee (string n, string id, string dept, string pos)
{
	name=n;
	idnumber=id;
	department=dept;
	position=pos;
}

void Employee()
{
	string name=" ";
	string idnumber = 0;
	string department = " ";
	string position = " ";
};

#endif 


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
31
32
33
34
35
36
//Employee Class Program
//By: Andrew A.

#include "EmployeeClass.h"
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
    MainWindow w;
    w.show();

    const int number = 3;

   Employee *objects[number];
   
   objects[0] = new Employee("Susan Meyers", "47899", "Accounting", "Vice President");
   objects[1] = new Employee("Mark Jones", "39119", "IT", "Programmer");
   objects[2] = new Employee("Roy Rogers", "81774", "Manufacturing", "Engineer");

    for(int i = 0; i < number; i++)
{
    cout << objects[i]->getNAME() << " ";
    cout << objects[i]->getID() << " ";
    cout << objects[i]->getDEPT() << " ";
    cout << objects[i]->getPOS() << endl;
};

    cin.ignore();
    cin.get();
	return 0;
}
"error C2065: 'QApplication' : undeclared identifier"
"error C2065: 'MainWindow' : undeclared identifier"

Well, that's pretty self-explanatory. Can you see where you've supplied definitions for those symbols? Because I certainly can't.

"error C2065: 'w' : undeclared identifier"
"error C3861: 'a': identifier not found"

If you haven't supplied a definition for MainWindow or QApplication, then w and a are undefined too.)

"error C2228: left of '.show' must have class/struct/union"

Again, that comes from the fact that you haven't defined MainWindow, so the compiler can't tell what type w is.

QApplication is a Qt class, so you'll need to include the appropriate header file for that. Presuambly, MainWindow is defined in some other header file that you've forgotten to include, too?

"error C2065: 'objects' : undeclared identifier"

That's probably a consequence of your earlier errors. Fix those, and then see if this one goes away too.

Last edited on
I looked those up, I dont have any QTWidgets. I'm assuming these headers are for the program QTCreator.

I'm going off from the code ak16 gave me however maybe theres another way to create a multiple arguments without using these definitions?

I believe my problem lies in just trying to create a definition for "objects" so I can initialize multiple arguments.
Therein lies the danger of copying and pasting other people's code without making an effort to understand what it does and how relevant it is to your own code.

ak16 was showing you an example program, including the constructor definition that you were missing. The right way to approach that would be to look at his/her code, understand what it does, understand which bits of it show you how to solve your problem, and implement something similar in your program.

Blindly copying and pasting doesn't help you learn, and it often doesn't get you working software either.

EDIT: By the way, I've just had another look at your header, and your multiple inclusion guard doesn't seem to be right. You have:

1
2
3
4
#ifndef Employee_Header
 // stuff...

#endif  


You don't have a line actually #defining that symbol, to prevent the header being included a second time.
Last edited on
I read both chapters that talk about classes in my book so I redid my code again.... I also fixed the header tip, thanks mikeyboy.

Everything is working out fine except that I'm getting an error on line 15, 16, and 17 on the main cpp file:

"expression must have (pointer-to-) function type"

I'm almost there, I just don't understand why I would need a pointer function... thanks for all the help in this thread btw!!

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//Employee Class Program
//Program by: Andrew A

#ifndef Employee_Header
#define Employee_Header
#include <string>

using namespace std;

class Employee
{
private:
	string name;
	int idnumber;
	string department;
	string position;
public:
	Employee (string n, int id)
	{
		name = n;
		id = idnumber;
		department = " ";
		position = " ";
	}

	Employee (string n, int id, string dept, string pos)
	{
		name = n;
		idnumber = id;
		department = dept;
		position = pos;
	}

	void setName(string n)
	{
		name = n;
	}

	void setID(int id)
	{
		idnumber = id;
	}

	void setDEPT(string dept)
	{
		department = dept;
	}

	void setPOS(string pos)
	{
		position = pos;
	}

	string getName() const
	{
		return name;
	}

	int getID() const
	{
		return idnumber;
	}

	string getDEPT() const
	{
		return department;
	}

	string getPOS() const
	{
		return position;
	}

};

#endif 


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
31
//Employee Class Program
//By: Andrew A.

#include "EmployeeClass.h"
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	const int NUM_OBJECTS = 3;
	Employee employee[NUM_OBJECTS] = {
		Employee("Susan Meyers", 47899, "Accounting", "Vice President"),
		Employee("Mark Jones", 39119, "IT", "Programmer"),
		Employee("Roy Rogers", 81774, "Manufacturing", "Engineer")
	};
	
	for (int i = 0; i < NUM_OBJECTS; i++)
	{
		cout << employee[i].getName();
		cout << employee[i].getID();
		cout << employee[i].getDEPT();
		cout << employee[i].getPOS() << endl;
	}

    cin.ignore();
    cin.get();
	return 0;
}


EDIT: found the problem, changed "Employee[NUM_OBJECTS]" in line 15 to "employee[NUM_OBJECTS]" cause i needed to differentiate it from class, and fixed "Employee[i]" to "employee[i]"

Thanks again guys for the help.
Last edited on
Topic archived. No new replies allowed.