Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. LNK2005 Error...

LNK2005 Error...

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rizean
    wrote on last edited by
    #1

    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.

    S M 2 Replies Last reply
    0
    • R Rizean

      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.

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      I'd guess you've got an include file without #include guards[^].

      Steve

      R 1 Reply Last reply
      0
      • R Rizean

        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.

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        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:

        R 1 Reply Last reply
        0
        • S Stephen Hewitt

          I'd guess you've got an include file without #include guards[^].

          Steve

          R Offline
          R Offline
          Rizean
          wrote on last edited by
          #4

          No, all the header files have #include guards, which is part of my confusion as I thought they were suppose to stop this kind of thing from happening.

          1 Reply Last reply
          0
          • M Mark Salsbery

            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:

            R Offline
            R Offline
            Rizean
            wrote on last edited by
            #5

            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:

            M 1 Reply Last reply
            0
            • R Rizean

              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:

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              What and where is "flush_istream" (since that's the one causing the error)? Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              R 1 Reply Last reply
              0
              • M Mark Salsbery

                What and where is "flush_istream" (since that's the one causing the error)? Mark

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                R Offline
                R Offline
                Rizean
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups