LNK2005 Error...
-
Hello all, While working on an ungraded project for class I came accross a LNK2005 Error which while I have been able to work around, I would like to know how to do it correctly. The project is to create an employee data base that has an employee class with child classes. That's the jest of the project. Employee - base class -Researcher - child class -Engineer - child class -Manager - child class Now the above four classes all include employee_template.h which defines a template function called void _getInput(std::string& message, T& result). Now the file containing function _getInput include stream_flush.h. Inside of stream_flush includes a function called flush_stream. The problem I am running into is that Employee and it's three child classes all include "employee_template.h" to get the function _getInput which include stream_flush.h to get the function flush_stream which is what generates the errors. engineer.obj : error LNK2005: "void __cdecl flush_istream(...) already defined in employee.obj I was able to get around the error by using the linker command line option: /FORCE:MULTIPLE but I am assuming this is not the right way.
-
Hello all, While working on an ungraded project for class I came accross a LNK2005 Error which while I have been able to work around, I would like to know how to do it correctly. The project is to create an employee data base that has an employee class with child classes. That's the jest of the project. Employee - base class -Researcher - child class -Engineer - child class -Manager - child class Now the above four classes all include employee_template.h which defines a template function called void _getInput(std::string& message, T& result). Now the file containing function _getInput include stream_flush.h. Inside of stream_flush includes a function called flush_stream. The problem I am running into is that Employee and it's three child classes all include "employee_template.h" to get the function _getInput which include stream_flush.h to get the function flush_stream which is what generates the errors. engineer.obj : error LNK2005: "void __cdecl flush_istream(...) already defined in employee.obj I was able to get around the error by using the linker command line option: /FORCE:MULTIPLE but I am assuming this is not the right way.
I'd guess you've got an include file without #include guards[^].
Steve
-
Hello all, While working on an ungraded project for class I came accross a LNK2005 Error which while I have been able to work around, I would like to know how to do it correctly. The project is to create an employee data base that has an employee class with child classes. That's the jest of the project. Employee - base class -Researcher - child class -Engineer - child class -Manager - child class Now the above four classes all include employee_template.h which defines a template function called void _getInput(std::string& message, T& result). Now the file containing function _getInput include stream_flush.h. Inside of stream_flush includes a function called flush_stream. The problem I am running into is that Employee and it's three child classes all include "employee_template.h" to get the function _getInput which include stream_flush.h to get the function flush_stream which is what generates the errors. engineer.obj : error LNK2005: "void __cdecl flush_istream(...) already defined in employee.obj I was able to get around the error by using the linker command line option: /FORCE:MULTIPLE but I am assuming this is not the right way.
Rizean wrote:
Employee and it's three child classes all include "employee_template.h"
They just include the same header file or they all derive from a class in employee_template.h? What does the code look like in employee.h and employee_template.h? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I'd guess you've got an include file without #include guards[^].
Steve
-
Rizean wrote:
Employee and it's three child classes all include "employee_template.h"
They just include the same header file or they all derive from a class in employee_template.h? What does the code look like in employee.h and employee_template.h? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
employee_template.h
#ifndef EMPLOYEE_TEMPLATE
#define EMPLOYEE_TEMPLATE#include <iostream>
#include <string>
#include "stream_flush.h"template <class T>
void _getInput(const std::string& message, T& result)
{
std::cout << message;
std::cin >> result;
flush_istream(std::cin);
if (std::cin.fail())
{
std::cin.clear();
std::cout << "Invalid input!\n";
_getInput(message, result);
}
}#endif
employee.h Just defines the base class and does not include employee_template.h however employee.ccp does. employee.ccp
#include "employee.h"
#include <string>
#include "employee_template.h"using namespace std;
Employee::Employee()
{
_getInput<string>("Please enter the employees first name: ", mFName);
// more of the same
}// _getInput is only used in the default constructor
}researcher.h
#ifndef RESEARCHER_H
#define RESEARCHER_H#include "employee.h"
#include <string>
#include <fstream>class Researcher: public Employee
{
public:
Researcher();
Researcher(/* lots of stuff here */);
// more of the same
};#endif
researcher.cpp
#include "researcher.h"
#include <string>
#include <fstream>
#include "employee_template.h"using namespace std;
Researcher::Researcher():Employee
{
_getInput<string>("Enter the researcgers school: ", mSchool);
// more of the same
}
//more of the same
};I hope this helps and if you need more let me know. BTW, is there a way to cut and paste on Codeproject? Edit: Errors from learning to post on CP... :wtf:
-
employee_template.h
#ifndef EMPLOYEE_TEMPLATE
#define EMPLOYEE_TEMPLATE#include <iostream>
#include <string>
#include "stream_flush.h"template <class T>
void _getInput(const std::string& message, T& result)
{
std::cout << message;
std::cin >> result;
flush_istream(std::cin);
if (std::cin.fail())
{
std::cin.clear();
std::cout << "Invalid input!\n";
_getInput(message, result);
}
}#endif
employee.h Just defines the base class and does not include employee_template.h however employee.ccp does. employee.ccp
#include "employee.h"
#include <string>
#include "employee_template.h"using namespace std;
Employee::Employee()
{
_getInput<string>("Please enter the employees first name: ", mFName);
// more of the same
}// _getInput is only used in the default constructor
}researcher.h
#ifndef RESEARCHER_H
#define RESEARCHER_H#include "employee.h"
#include <string>
#include <fstream>class Researcher: public Employee
{
public:
Researcher();
Researcher(/* lots of stuff here */);
// more of the same
};#endif
researcher.cpp
#include "researcher.h"
#include <string>
#include <fstream>
#include "employee_template.h"using namespace std;
Researcher::Researcher():Employee
{
_getInput<string>("Enter the researcgers school: ", mSchool);
// more of the same
}
//more of the same
};I hope this helps and if you need more let me know. BTW, is there a way to cut and paste on Codeproject? Edit: Errors from learning to post on CP... :wtf:
What and where is "flush_istream" (since that's the one causing the error)? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
What and where is "flush_istream" (since that's the one causing the error)? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
One of my instructors finally got back with me and explained to me what I was doing wrong. "flush_istream" was a function I created to deal with invalid user inputs to cin calls. The function is only five lines long and it is used by a number of other functions. I placed it in its own header files stream_flush.h, but because it was the only function and because it was so short I did not separate the definition and implementation, i.e. no stream_flush.cpp file. However, as you have probably already figured out this caused the error of it being defined multiable times. My instructor suggested I inline the function which did fix the problem, however I'm thinking it would be better to separate the definition and the implementation.
#ifndef STREAM_FLUSH_H
#define STREAM_FLUSH_H#include <istream>
//blocking if no data in the stream
inline void flush_istream(std::istream& in)
{
while(in.peak() != '\n')
in.get();
}#endif
Thanks all for your help. Being in Korea often leaves me sitting here for up to a day or more sometimes waiting for help. Just one of the challenges of being in the Air Force and going to school online.