fatal error LNK1120: and error LNK2001:

here is my 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//.h file code:

#include <string>
#include <iostream>

class Employee
	{
		//attributes
	private:
		std::string name; // instance variable object variable)
		static int totalemployees; // class variables

		//constructors
	public:
		Employee();
		Employee(std::string name);


		//behaviors
		//NONE

		//properties (or Get/Set methods)
		std::string getName();
		void setName(std::string value);

		static int getTotalEmployees();
		static void setTotalEmployees(int value);
	};
class Program
	{
		static void Main(std::string args[]);
	};

//.cpp file code:

Employee::Employee()
{
	//set default value
	name = "na";
	totalemployees++; // totalEmployee =TotalEmployee +1;
}

Employee::Employee(std::string name)
{
	//set given value to the object
	this->name = name;
	totalemployees++; //increment class variable
}

std::string Employee::getName()
{
	return name;
}

void Employee::setName(std::string value)
{
	name = value;
}

int Employee::getTotalEmployees()
{
	return totalemployees;
}

void Employee::setTotalEmployees(int value)
{
	totalemployees = value;
}

static void main(std::string args[])
{

	// create two Employee objects
	Employee *emp1 = new Employee();

	std::cout << "Employee 1: " << emp1->getName() << std::endl;
	std::cout << "Total Employees: " << Employee::getTotalEmployees() << std::endl;

	Employee *emp2 = new Employee("Rick");
	std::cout << "Employee 2: " << emp2->getName() << std::endl;
	std::cout << "Employee 2: " << Employee::getTotalEmployees() << std::endl;
}


Error Log
1>InstanceVariableVersusClassVariable.obj : error LNK2001: unresolved external symbol "private: static int Employee::totalemployees" (?totalemployees@Employee@@0HA)
\Projects\InstanceVariableVersusClassVariable\Debug\InstanceVariableVersusClassVariable.exe : fatal error LNK1120: 1 unresolved externals

1>InstanceVariableVersusClassVariable - 2 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Desired Output:
Employee 1: na
Total Employees: 1
Employee 2: Rick
Employee 2: 2
Press any key to continue . . .
working code see line 31
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
84
85
//.h file code:

#include <string>
#include <iostream>

using namespace std;

class Employee
	{
		//attributes
	private:
		std::string name; // instance variable object variable)
		static int totalemployees; // class variables

		//constructors
	public:
		Employee();
		Employee(std::string name);


		//behaviors
		//NONE

		//properties (or Get/Set methods)
		std::string getName();
		void setName(std::string value);

		static int getTotalEmployees();
		static void setTotalEmployees(int value);
	};
int Employee::totalemployees = 0;//forgot to declare class variable to 0
class Program
	{
		static void Main(std::string args[]);
	};

//.cpp file code:

Employee::Employee()
{
	//set default value
	name = "na";
	totalemployees++; // totalEmployee =TotalEmployee +1;
}

Employee::Employee(std::string name)
{
	//set given value to the object
	this->name = name;
	totalemployees++; //increment class variable
}

std::string Employee::getName()
{
	return name;
}

void Employee::setName(std::string value)
{
	name = value;
}

int Employee::getTotalEmployees()
{
	return totalemployees;
}

void Employee::setTotalEmployees(int value)
{
	totalemployees = value;
}

static void main()
{

	 //create two Employee objects
	Employee *emp1 = new Employee();

	std::cout << "Employee 1: " << emp1->getName() << std::endl;
	std::cout << "Total Employees: " << Employee::getTotalEmployees() << std::endl;

	Employee *emp2 = new Employee("Rick");
	std::cout << "Employee 2: " << emp2->getName() << std::endl;
	std::cout << "Employee 2: " << Employee::getTotalEmployees() << std::endl;
}


Output:
Employee 1: na
Total Employees: 1
Employee 2: Rick
Employee 2: 2
Press any key to continue . . .
Last edited on
Topic archived. No new replies allowed.