Yet another linker error (class declarations included)
Im getting this error in my code
Quote:
[Linker Error] Unresolved external 'Employee::Employee(_STL::basic_string<char, _STL::char_traits<char>, _STL::allocator<char> >, _STL::basic_string<char, _STL::char_traits<char>, _STL::allocator<char> >, bool, int)' referenced from C:\SECURITY\MAIN.OBJ
See Updated Code Below
I think the error is on the last line in my main code, "Vector.pushback( new Employee( S, F, Auth, ID ));"
I can't see a single error in those class declarations? Can anyone tell me whats wrong?
Re: Yet another linker error (class declarations included)
I do not get the problem with the following code:
// Employee.h
Code:
#ifndef EMP_H
#define EMP_H
#include <string>
class Employee {
public:
Employee( std::string S, std::string F, bool Auth, int ID );
std::string Surname;
std::string Forename;
bool Authorised;
int IDNo;
};
#endif
Employee.cpp
Code:
#include "employee.h"
Employee::Employee( std::string S, std::string F, bool Auth, int ID ) // Default Constructor
{
Surname = S;
Forename = F;
Authorised = Auth;
IDNo = ID;
}
MAINProg.cpp
Code:
#include <vector>
#include <string>
#include "employee.h"
std::vector<Employee*> Vector;
int main()//int argc, char* argv[]
{
int choice, ID, maxID = 1;
std::string S, F;
bool Auth;
char yesno;
// You know this is an illegal access, right?
Vector[0] = new Employee( S, F, Auth, ID );
}
There are other problems with the code you posted. First, you should change your prototype to the following:
Code:
Employee( const std::string& S, const std::string& F, bool Auth, int ID );
This eliminates the copying that the compiler would do when you are passing the strings to your function.
The second problem (unless you left out code) is that you are attempting to set Vector[0] to a value. Your vector has no element 0, it doesn't have any elements. Therefore what you are doing will cause an access violation. Either size the vector appropriately before using it, or use push_back() to add an item to the vector. You can either size a vector by utilizing the vector's constructor that takes an int argument, or by calling resize().
I would also call it something else besides "Vector". "EmployeeList" or "EmployeeVector" is much more descriptive.
Code:
Vector.resize(1); // Makes sure that Vector has one element available
Regards,
Paul McKenzie
Re: Re: Yet another linker error (class declarations included)
Quote:
Originally posted by Paul McKenzie
There are other problems with the code you posted. First, you should change your prototype to the following:
Code:
Employee( const std::string& S, const std::string& F, bool Auth, int ID );
This eliminates the copying that the compiler would do when you are passing the strings to your function.
Hi Paul,
I followed your advice and changed my code to "pushback" and changed the name of the vector. I tried your suggestion of using const std::string etc etc.. but was presented with a few errors.
I would like to use this method of pass by reference but when you say prototype do you mean in main? The header file or the Employee.cpp file?
If you don't get this error can you suggest any reason why I might? I have tried making a new "project" but the errors still occur.
Re: Re: Re: Yet another linker error (class declarations included)
Quote:
Originally posted by GraemeW
Hi Paul,
I followed your advice and changed my code to "pushback" and changed the name of the vector. I tried your suggestion of using const std::string etc etc.. but was presented with a few errors.
What were the errors?
Quote:
I would like to use this method of pass by reference but when you say prototype do you mean in main? The header file or the Employee.cpp file?
I don't know what you mean by "in main()". The main() is just a function.
The prototype is where the function is declared. For class member functions, this is found within the class definition. In Employee.h, you've defined the constructor as taking std::string arguments. In your implementation in Employee.cpp, the constructor matches what is in Employee.h. When you changed this to const std::string&, did you do it so that the .h matches the .cpp? Is this what the error is?
Code:
//Employee.h
class Employee
{
public:
Employee(const std::string& s1, const std::string& s2,
bool b, char c);
};
// Employee.cpp
Employee::Employee(const std::string& s1, const std::string& s2,
bool b, char c)
{
//
}
Also, the correct standard header is <iostream>, not iostream.h
Another thing -- any reason why you need to create a vector<Employee*> instead of vector<Employee>? This latter will relieve you from having to use new Employee(...) and will make your code easier to handle since you will not have to be responsible for cleanup (calling delete) at the end.
Regards,
Paul McKenzie