我遇到了未定义的引用错误问题。我收到以下错误:
[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
我正在使用Bloodshed dev 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;
}
答案 0 :(得分:4)
您几乎肯定忘记在构建过程中包含ClassVoter.cpp
。
我从未使用过Bloodshed dev C ++,因此无法提供逐步说明。
答案 1 :(得分:1)
undefined reference
表示链接器无法在您链接在一起创建可执行文件的任何文件中找到程序使用的某些函数的定义。
使用g ++编译器,这意味着您将提供如下命令:
g++ main.cpp;
而不是:
g++ main.cpp ClassVoter.cpp;