This one is absolutely amazing. I don't get how this bug is even possible. My program tries to read in class data from a formatted text file. Somehow all my classes data gets changed to the same piece of data in a file. I'll explain more at the bottom
Agent Class: (pay attention to operator>>, Thats where my problem begins.)
Here's where I call operator>>:Code:class agent { public: agent(); agent(int idNum, string clntNm, string clntFT, string pTC, agent *next); agent(const agent&); ~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(); agent operator=(agent&); friend ifstream& operator>>(ifstream&,agent&); friend ofstream& operator<<(ofstream&,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(): idNumber(0), nextAgent(0) { string str = " "; clientName = str; clientFileType = str; pathToClient = str; } //copy constructor agent::agent(const agent& rhs): idNumber(rhs.getIdNum()), nextAgent(rhs.getNextAgent()), clientName(rhs.getClientName()), clientFileType(rhs.getFileType()), pathToClient(rhs.getPath()) {} 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; } agent agent::operator=(agent &agentFull) //..::!untested!::.. { clientName = agentFull.getClientName() ; idNumber = agentFull.getIdNum(); clientFileType = agentFull.getFileType(); pathToClient = agentFull.getPath(); nextAgent = 0; return *this; } ifstream& operator>>(ifstream& rfin,agent &rnext) { char ch; string s1,s2,s3,sid; while (rfin.get(ch)) //read a character { // cout<<"\'"<<ch<<"\'" <<endl; if (ch == 'a') //is it the begining of an agent { rfin.get(ch); //eat up newline getline(rfin,s1, '\n'); getline(rfin,s2, '\n'); getline(rfin,s3, '\n'); cerr<<"*!*"<<s1<<"!"<<s2<<"!"<<s3<<"*!*"; //data is fine rnext.setIdNum(GLOBALid + 1); //define agent GLOBALid++; //don't want to assign all agents with an id of 1 rnext.setClientName(s1); //Problem lies in these rnext.setFileType(s2); rnext.setPath(s3); rnext.setNextAgent(0); cerr<<"!"<<rnext.getPath(); //agent is bad here return rfin; }//if }//while return rfin; } ofstream& operator<<(ofstream& lhs,agent* rhs) { lhs <<'a' << '\n'; //formatting lhs << rhs->getClientName() << '\n'; lhs << rhs->getFileType() << '\n'; lhs << rhs->getPath() << '\n'; lhs << '\n'; //formatting return lhs; }
here's the text file it reads from:Code:fileManager::fileManager() {} //..::FUNCTION file2Agent()::.. //reads a file and creates a vector of agents (not pointers to) vector <agent>& fileManager::file2Agent(vector<agent> &parray) { agent empty; empty.setIdNum(0); ifstream fin("agents.DATA"); if(!fin) //did we get the file open? { cerr<<endl<<endl<<"\aCouldn't open file in vector <agent>& fileManager::file2Agent()"; //nope return parray; }//if while (fin >> empty) //get an agent { cerr<<empty.getPath(); parray.push_back(empty); //put it into a vector }//if fin.close(); return parray; }//file2Agent
Code:a this is testing1 a this is testing2 a this is testing3 a this is testing4 a this is testing5 a this is testing6
in operator>>:The program reads in the data from the file correctly but after it sets the agents values with the data the agents all get set with the first piece of data read in for that agent.
if I display the list of agents here's what happens:
or if the file looks like this:Code:1 this| this|this 2 this| this|this 3 this| this|this
Code:a asdf gfs gfhtr a tye nf dfg a er vfd ew
the output of the list looks like this:
some how either the set functions in the agent class have gone crazy or (more likely) the get functions are all returning the same variable. I don't get how this would happen and no matter how many times I go through my code I don't get it. I've spent 3 days on this bug now and I've run out of Ideas.Code:1 asdf|asdf|asdf 2 tye|tye|tye| 3 er| er| er



LinkBack URL
About LinkBacks



I used to be an adventurer like you... then I took an arrow to the knee.
CornedBee