0

I am having a problem with undefined reference errors. I get the following errors:

[Linker error] undefined reference to `operator>>(std::istream&, Voter&)'
[Linker error] undefined reference to `Voter::~Voter()' 
[Linker error] undefined reference to `Voter::~Voter()'
ld returned 1 exit status 

I am using Bloodshed dev C++ 4.9

My code:

Voter.h

#include <iostream>
#ifndef VOTER_H
#define VOTER_H

using namespace std;
class Voter
{
      private:
              string ID;
              int nr_times_voted;
              bool voted;

      public:
             Voter()
             {
               ID = " ";
               nr_times_voted = 0;
               voted = false;
             }

             Voter(string newVoter)
             {
               ID = "0000";
               nr_times_voted = 0;
               voted = false;
             }

              ~Voter();     
              string getID();
              int getnr_times_voted();
              bool getvoted();
              void set_voted()
              {
                voted = true;
              }

               friend Voter operator++(Voter& V);
               friend istream & operator>>(istream & ins, Voter & V);
               friend ostream & operator<<(ostream & outs, Voter & V);
};
#endif

ClassVoter.cpp

    #include "Voter.h"
    #include <iostream>
    #include <string>

    using namespace std;

    //Accessors to retrieve data from each of the member variables
    string Voter:: getID()
    {
      return ID;
    }

    int Voter:: getnr_times_voted()
    {
      return nr_times_voted;
    }

    bool Voter:: getvoted()
    {
      return voted;
    }

    //destructor
    Voter::~Voter()
    {}

    void operator++(voted & V)
    { 
      voted++;
      return voted;    
    }

    istream & operator >>(istream & ins, Voter & V)
    {
      ins >> V.ID >> V.nr_times_voted >> V.voted;
    }


    ostream & operator <<(ostream & outs, Voter & V)
    {
      outs << " Voters that want to vote: " << endl;
      outs << V.ID << endl;
      return outs;
    }

TestVoter.cpp

#include "Voter.h"
#include <iostream>
#include <fstream> 
#include <cstdlib> 
#include <string>

using namespace std;

int main()
{
   ifstream infile;
   ofstream outfile;
   Voter ID;

   cout << "Enter your Voter ID: ";
   cin >> ID;

   infile.open("VotersRoll.dat");
   outfile.open("UpdatedVoters.dat");

   infile.close();
   outfile.close();   

   system("pause");
   return 0;
} 
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
user1291092
  • 105
  • 4
  • 9
  • 1
    You're not linking with the object file produced from `ClassVoter.cpp`. Do. – gspr Mar 29 '13 at 17:00
  • can you post lines before "[Linker error] undefined reference to `operator>>(std::istream&, Voter&)'", which arguments were passed to ld? – MLeblanc Mar 29 '13 at 17:00
  • You might try linking the ClassVoter.o object file into your final image. – WhozCraig Mar 29 '13 at 17:00
  • Please [don't use `system("pause")` in your programs](http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong). – Nik Bougalis Mar 29 '13 at 17:05
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?: Failure to link against appropriate object files](http://stackoverflow.com/a/12574400/902497) – Raymond Chen Oct 01 '13 at 13:13

2 Answers2

4

You are almost certainly forgetting to include ClassVoter.cpp in your build process.

I've never used Bloodshed dev C++, so can't offer step-by-step instructions.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

undefined reference means the linker can't find the definition of some function used by the program in any of the files you're linking together to create the executable.

With g++ compiler, this means you're giving a command like this:

g++ main.cpp;

instead of:

g++ main.cpp ClassVoter.cpp;
blue
  • 2,683
  • 19
  • 29