here's the agent class definition with the overloaded operator:
Code:
class agent
{
public:
agent(){}
agent(int idNum, string clntNm, string clntFT, string pTC, agent *next);
~agent(){listLength--;}
int getIdNum() const {return idNumber;}
void setIdNum(int newId) {idNumber = newId;}
string getClientName() const {return clientName;}
void setClientName(string newClientName) {clientName = newClientName;}
string getFileType() const {return clientName;}
void setFileType(string newFileType) {clientFileType = newFileType;}
string getPath() const {return clientName;}
void setPath(string newFilePath) {pathToClient = newFilePath;}
agent *getNextAgent() const {return nextAgent;}
void setNextAgent(agent *newNextAgent) {nextAgent = newNextAgent;}
void setNextAgent(agent &newNextAgent) {nextAgent = &newNextAgent;}
void display();
friend ifstream& operator>>(ifstream&,agent*);
static int listLength;
private:
int idNumber;
agent *nextAgent; //a pointer to the next agent
string clientName; //its clients name
string clientFileType; //and its clients file type
string pathToClient; //where client is located on Hard Disk
};
int agent::listLength = 0;
agent::agent(int idNum, string clntNm, string clntFT, string pTC, agent *next): idNumber(idNum),
clientName(clntNm), clientFileType(clntFT), pathToClient(pTC)
{
listLength++;
nextAgent = next;
}//agent::agent(int,string,string,string,agent*)
void agent::display()
{
cout<<idNumber << "\t" <<clientName<< "|\t" << clientFileType << "|\t"<< pathToClient;
cout<< "..." << endl;
}
ifstream& operator>>(ifstream& rfin,agent &rnext)
{
char ch;
int id;
string s1,s2,s3,sid;
while (rfin.get(ch)) //read a character
{
if (ch = 'a') //is it the begining of an agent
{
rfin.get(ch); //eat up newline
getline(rfin,sid);//if changing agent class, mod these
getline(rfin,s1);
getline(rfin,s2);
getline(rfin,s3);
id = atoi(sid.c_str());
rnext.setIdNum(id); //define agent
rnext.setClientName(s1);
rnext.setFileType(s2);
rnext.setPath(s3);
rnext.display(); //agent successfully read in here and displays correctly
}//if
}//while
return rfin;
}
here's the fileManager class with the file 2 agent definition:
Code:
class fileManager
{
public:
fileManager();
~fileManager() {}
agent *file2Agent();
void agent2File(agent *phead);
long getFileSize(ifstream *pin);
private:
};
fileManager::fileManager()
{}
//..::FUNCTION file2Agent()::..
//reads a file and creates a list of agents out of that
agent *fileManager::file2Agent()
{
cerr<<"mark 3";
agent *phead= 0;
cin.get();
ifstream fin("agents.DATA");
if(!fin) //did we get the file open?
{
cerr<<endl<<endl<<"\aCouldn't open file in agent *fileManager::file2Agent()"; //nope
return phead;
}//if
vector <agent> parray;
agent empty;
while (fin >> empty) //gets an agent successfully butt...
{
parray.push_back(empty); //this code still never executes
}//if
cin.get();
fin.close();
vector2Agent(parray,phead);
cerr<<endl<<endl;
phead->display();
return phead;
}//file2Agent
here's where the access violation is:
Code:
//..::FUNCTION vector2Agent::..
//turns an vector into a singly linked list
void vector2Agent(vector<agent> &parray, agent *&phead)
{
phead = &parray[0]; //
agent *pnext = phead;
cerr<<parray.size();
if (parray.size() == 0)
{
phead = 0;
return;
}
for(unsigned int x = 0; x < parray.size(); x++)
{
pnext->setNextAgent(&parray[x]);
if (parray.size() >= 2)
pnext = pnext->getNextAgent();
}
pnext->setNextAgent(0); //pnext doesn't exist so access violation raised here.
}
The text file contains the formatted text of 1 agents worth of data.
Thanks again