error LNK2019: How to locate the error

I wrote this code and I am getting the linker error.

Searched online and found some documentation on msdn.microsoft.com. However, I could not locate where error happened.




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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class employee
{
private:
    string _name1;
	int _age;
	float _salary;



public:

	employee(); //default constructor

    employee(string name, int a, float sal)
    {
        _name1 = name;
		_age = a;
		_salary =  sal;
    }

    // Copy constructor
    employee(const employee &cSource)
    {
        _name1 = cSource._name1;
		_age = cSource._age;
		_salary = cSource._salary;
    }

	employee::~employee()
	{
		cout<<"\n Deleting the info.\n";
		_name1.erase();
		//_age.erase();
		//_salary.erase();

		//Destructor will not delete an array of pointer.
		//Destructor will be used to delete the members only.
		// and that too pointer to members.
		//We do not need to delete the age and salary as these are not pointers.
	}

	void display();
	//float increment_the_salary(float _salary, float PercentageIncrease);  // why salary has to be declared float again

};

void employee::display()
{
	cout<<"\n Name is "<<_name1;
	cout<<"\n age is "<<_age;
	cout<<"\n salaray is "<<_salary;
}

/*
float employee::increment_the_salary(float _salary, float PercentageIncrease)
{
	float _salary =  _salary*(1+PercentageIncrease/100);
	return _salary;
}*/


int main()
{

	string name1;
	int age, option, n = 0;
	float salary;

	//Create array of point
	employee *EmployeeList[10];

	cout<<"You can enter upto 10 employees \n";
	//option = 1;
	//Enter employees information


	do
	{
		cout<<"Enter the name\n";
		cin>>name1;
		cout<<"Enter the age \n";
		cin>>age;
		cout<<"Enter the salary \n";
		cin>>salary;

		EmployeeList[n] = new employee; 
		employee temp(name1, age, salary);
		*EmployeeList[n]= temp;

		//employee *temp= new employee(name1, age, salary);
		//EmployeeList[n] = temp;

		++n;
		cout<<"Do you enter one more employee \n";
		cout<<"1 for Yes; 0 for No";
		cin>>option;

	}while(option);


	//Destructor is not created
	//explicitly destructing

	//Now printing all employee's information
	for (int i=0; i<n; ++i)
	{
		EmployeeList[i]->display();
		delete EmployeeList[i];
	}

	system("pause");
	return 0;
}
Are you calling an undefined function anywhere? Is your function definition getting compiled/linked properly? Its hard to tell whats going on without the complete error message.
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
class employee
{
private:
	string _name1;
	int _age;
	float _salary;
public:
	employee(); //default constructor (never implemented)

    employee(string name, int a, float sal)
    {
		_name1 = name;
		_age = a;
		_salary =  sal;
    }

    employee(const employee &cSource) //does the default behaviour (no need)
    {
		_name1 = cSource._name1;
		_age = cSource._age;
		_salary = cSource._salary;
    }

	employee::~employee()
	{
		cout<<"\n Deleting the info.\n";
		//_name1.erase(); //no need. When the class die, all its elements die
		//You need to handle here the life time of raw pointers
	}

	void display();
	//float increment_the_salary(float _salary, float PercentageIncrease);  // why salary has to be declared float again

};


employee *EmployeeList[10]; I hope you've got a good reason (polymorphism) to use pointers. However, ¿why an array instead of a vector? (especially since you are keeping track of the size).

1
2
3
4
//EmployeeList[n] = new employee; 
//employee temp(name1, age, salary);
//*EmployeeList[n]= temp;
EmployeeList[n] = new employee(name1, age, salary); 


system("pause");
http://www.cplusplus.com/forum/articles/11153/
http://www.cplusplus.com/forum/articles/7312/
Thanks ne555 for helping me.

Since I am new to C++, could you please explain why

EmployeeList[n] = new employee(name1, age, salary);

and why not

EmployeeList[n] = new employee;
employee temp(name1, age, salary);
*EmployeeList[n]= temp;


Also, I am trying to use "array of pointer" here. Could you elaborate why I should be using vector of pointer and how?
1
2
3
EmployeeList[n] = new employee; //the employee must have a default constructor.
employee temp(name1, age, salary);
*EmployeeList[n]= temp; //copying the object (discarding what the default constructor did) 
The copy could be inefficient, and a class could not have a default constructor.
Besides EmployeeList[n] = new employee(name1, age, salary); is more compact and is clear what is the result (don't have to look up to what is temp)

The question remains, ¿why do you use pointers?
The idea is that a vector can growth dynamically, but you don't need to worry about memory management. http://www.cplusplus.com/reference/stl/vector/
std::vector<employee> EmployeeList; creating a vector
EmployeeList[n]; accessing an element (the index must be valid, between 0 and size-1)
EmployeeList.push_back( employee(name1, age, salary) ); adding an employee (increasing the size)
Employee.size(); checking the size
Thanks a lot for explaining.

Also, could you please explain: how did you figure out which portion of my code was giving me linking problem?
Topic archived. No new replies allowed.