2013-03-29 160 views
0

我遇到未定義的參考錯誤。我收到以下錯誤:未定義的參考錯誤

[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 

我使用的流血開發的C++ 4.9

我的代碼:

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; 
} 
+1

你不是從'ClassVoter.cpp生成的目標文件鏈接'。做。 – gspr

+0

你可以在「[鏈接器錯誤]未定義的引用'運營商>>(std :: istream&,Voter&)'」之前發佈行,哪些參數傳遞給ld? – MLeblanc

+0

您可以嘗試將ClassVoter.o目標文件鏈接到最終圖像中。 – WhozCraig

回答

4

你幾乎可以肯定忘記包括在構建過程ClassVoter.cpp

我從來沒有用過Bloodshed dev C++,所以不能提供分步說明。

+1

目標文件通過編譯'ClassVoter.cpp'生成,而不是? – gspr

+1

+1,如果不是徹底編譯它。 – WhozCraig

1

undefined reference意味着鏈接程序無法在您鏈接在一起創建可執行文件的任何文件中找到該程序使用的某些函數的定義。

隨着G ++編譯器,這意味着你給這樣的命令:

g++ main.cpp; 

代替:

g++ main.cpp ClassVoter.cpp;