我遇到未定義的參考錯誤。我收到以下錯誤:未定義的參考錯誤
[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;
}
你不是從'ClassVoter.cpp生成的目標文件鏈接'。做。 – gspr
你可以在「[鏈接器錯誤]未定義的引用'運營商>>(std :: istream&,Voter&)'」之前發佈行,哪些參數傳遞給ld? – MLeblanc
您可以嘗試將ClassVoter.o目標文件鏈接到最終圖像中。 – WhozCraig