c++ - list node pointer 'not declared in this scope'? -
c++ - list node pointer 'not declared in this scope'? -
i've searched high , low trying debug this, far can't figure out.
i've got class declaration in file named datalist.h, methods in file named datalist.cc file, , main programme in separate .cc file.
datalist.cc has #include header include datalist.h.
whenever run
g++ datalist.cc
in ubuntu terminal, spits out
datalist.cc: in function ‘bool transactonce(int&, trans&, std::string&)’: datalist.cc:395:2: error: ‘listnodet’ not declared in scope datalist.cc:395:13: error: ‘nodetptr’ not declared in scope datalist.cc:396:13: error: ‘headtrans’ not declared in scope
what's weird declare variables of these type on place in other fellow member functions in datalist.cc, compiler doesn't give me other errors besides above. i'm trying figure out g++ doesn't particular declarations in 1 function.
'listnodet' struct within main class, , struct includes pointer object struct type. 'nodetptr' pointer 'listnodet' object, , 'headtrans' pointer (contained privately in class) points object of type listnodet, particularly, first node in linked list.
the class "list" intended linked lists.
here's "datalist.h" file:
#ifndef datalist_h #define datalist_h #include <iostream> #include <fstream> #include <ostream> #include <iomanip> using namespace std; const int maxdisciplines = 4; const int maxbreeds = 4; struct horse { int id; string name; // maximum 25 alphanumeric characters, spaces included double height;// in inches? int age; char gendercode;//'m', 'f', 'm', or 'f' int breed; int disciplinecount; int discipline[maxdisciplines]; double lowbid; // minimum selling cost }; struct trans { char transcode; //'a' add, 'd' delete or updating disciplines, or 'p' updating lowbid cost horse transhorse; }; class list { private: struct listnodet { trans trans; struct listnodet *nextlnt; }; listnodet *headtrans; struct listnodeh { horse horse; struct listnodeh *nextlnh; }; listnodeh *headhorse; public: //listnodeh *headhorse; //listnodet *headtrans; list(); list(const list &obj); ~list(); bool inserthorse(horse newhorse); bool inserttrans(trans newtrans); bool deletehorse(int target); bool deletetrans(int target); bool viewhorse(int target, horse &viewhorse); bool viewtrans(int target, trans &viewtrans); int getnumhorses(); void getnumtrans(int &numa, int &numdel, int &numdisc, int &nump); bool transactonce(int &skip, trans &thistrans, string &failstring); bool updatedisc(trans thistrans); bool updateprice(trans thistrans); void reportonetransaction(fstream &strm, trans thistrans); }; //end list class definition //methods in file datalist.cc , compiled separately. #endif
and below "datalist.cc" file. problematic "transactonce()" towards bottom.
#include "datalist.h" #include <iostream> #include <sstream> #include <fstream> #include <ostream> #include <iomanip> using namespace std; //constructor list::list(){ headhorse = null; headtrans = null; } //copy constructor list::list(const list &obj){ //copy linked list of horses listnodeh *nodehptr; listnodeh **newlisthptr; headhorse = null; newlisthptr = &headhorse; //means &(this->headhorse), not &(obj.headhorse) nodehptr = obj.headhorse; while (nodehptr != null) { *newlisthptr = new listnodeh; (*newlisthptr)->horse = nodehptr->horse; (*newlisthptr)->nextlnh = null; newlisthptr = &((*newlisthptr)->nextlnh); nodehptr = nodehptr->nextlnh; } //copy linked list of transactions listnodet *nodetptr; listnodet **newlisttptr; headtrans = null; newlisttptr = &headtrans; nodetptr = obj.headtrans; while (nodetptr != null) { *newlisttptr = new listnodet; (*newlisttptr)->trans = nodetptr->trans; (*newlisttptr)->nextlnt = null; newlisttptr = &((*newlisttptr)->nextlnt); nodetptr = nodetptr->nextlnt; } }//end re-create constructor //destructor list::~list(){ //destroy list of horses listnodeh *nodehptr; listnodeh *nexthnode; nodehptr = headhorse; while(nodehptr != null){ nexthnode = nodehptr->nextlnh; delete nodehptr; nodehptr = nexthnode; }//end walkthrough //destroy list of transactions listnodet *nodetptr; listnodet *nexttnode; nodetptr = headtrans; while(nodetptr != null){ nexttnode = nodetptr->nextlnt; delete nodetptr; nodetptr = nexttnode; }//end walkthrough }//end list::~list() //******************************************************************** // function name: inserthorse() // function purpose: inserts new horse struct our list // input parameters: // 1. horse newhorse - object of type info inserted list. //return value – bool //******************************************************************** bool list::inserthorse(horse newhorse){ bool success = false; listnodeh *newnodehptr; listnodeh *nodehptr; listnodeh *prevnodehptr = null; newnodehptr = new listnodeh; newnodehptr->horse = newhorse; if(!headhorse){ //if head==null, our list empty headhorse = newnodehptr; newnodehptr->nextlnh = null; success = true; } else { //perhaps add? our list not empty. nodehptr = headhorse; //prevnodeptr; [sic] in notes. think it's supposed prevnodeptr = null;, should true while(nodehptr != null && nodehptr->horse.id < newhorse.id){ //searches approriate place prevnodehptr = nodehptr; nodehptr = nodehptr->nextlnh; } if(nodehptr != null && nodehptr->horse.id == newhorse.id){ //checks duplicates delete newnodehptr; success = false; //note: way insert homecoming false } else { //insert node success = true; newnodehptr->nextlnh = nodehptr; if(prevnodehptr == null) //we're @ origin of list headhorse = newnodehptr; else //not @ origin of list prevnodehptr->nextlnh = newnodehptr; }//successful insertion } // delete newnodehptr; homecoming success; } //end list::inserthorse() //******************************************************************** // function name: inserttrans() // function purpose: inserts new trans struct our list. // if transaction trying delete or update horse not exist, // returns false, , transaction not inserted. // input parameters: // 1. trans newtrans - object of type trans inserted list. //return value – bool //******************************************************************** bool list::inserttrans(trans newtrans){ bool success = false; listnodet *newnodetptr; listnodet *nodetptr; listnodet *prevnodetptr = null; newnodetptr = new listnodet; newnodetptr->trans = newtrans; horse dummyhorse; //dummy variable passed reference viewhorse() //we need ensure few things //no duplicate ids in transactions //no del, disc, or p transactions horse dosn't exist in inventory if(!headtrans){ //if head==null, our list empty headtrans = newnodetptr; newnodetptr->nextlnt = null; success = true; } else { //perhaps add? our list not empty. nodetptr = headtrans; //prevnodeptr; [sic] in notes. think it's supposed prevnodeptr = null;, should true while(nodetptr != null && nodetptr->trans.transhorse.id < newtrans.transhorse.id){ //searches approriate place prevnodetptr = nodetptr; nodetptr = nodetptr->nextlnt; } //check duplicates if(nodetptr != null && nodetptr->trans.transhorse.id == newtrans.transhorse.id){ delete newnodetptr; success = false; } //in case of delete or update transactions horse not exist in inventory else if((newtrans.transcode == 'd' || newtrans.transcode == 'p') && !list::viewhorse(newtrans.transhorse.id, dummyhorse)){ delete newnodetptr; success = false; } else { //insert transaction node success = true; newnodetptr->nextlnt = nodetptr; if(prevnodetptr == null) //we're @ origin of list headtrans = newnodetptr; else //not @ origin of list prevnodetptr->nextlnt = newnodetptr; }//successful insertion } // delete newnodetptr; homecoming success; }//end list::inserttrans() //******************************************************************** // function name: deletehorse() // function purpose: deletes horse struct our object of class list. // input parameters: // 1. int target - id of object of type horse deleted list //return value – bool //******************************************************************** bool list::deletehorse(int target){ bool success = false; listnodeh *nodehptr; listnodeh *prevhptr; if (!headhorse) //empty list success = false; else if (headhorse->horse.id == target) { //found target @ origin of list, delete nodehptr = headhorse->nextlnh; delete headhorse; headhorse = nodehptr; success=true; } else { // maybe delete? nodehptr = headhorse; while(nodehptr!=null && nodehptr->horse.id < target){ //searching... prevhptr = nodehptr; nodehptr = nodehptr->nextlnh; } if(nodehptr == null && nodehptr->horse.id != target) //we're @ end of list, , if target's not here... success=false; //deletion failed. case delete() returns false. else { //we're between origin , end, , found target prevhptr->nextlnh = nodehptr->nextlnh; delete nodehptr; //delete target. success = true; } } homecoming success; } //******************************************************************** // function name: deletetrans() // function purpose: deletes trans struct our object of class list. // input parameters: // 1. int target - id of object of type trans deleted list //return value – bool //******************************************************************** bool list::deletetrans(int target){ bool success = false; listnodet *nodetptr; listnodet *prevtptr; if (!headtrans) //empty list success = false; else if (headtrans->trans.transhorse.id == target) { //found target @ origin of list, delete nodetptr = headtrans->nextlnt; delete headtrans; headtrans = nodetptr; success=true; } else { // maybe delete? nodetptr = headtrans; while(nodetptr!=null && nodetptr->trans.transhorse.id < target){ //searching... prevtptr = nodetptr; nodetptr = nodetptr->nextlnt; } if(nodetptr == null && nodetptr->trans.transhorse.id != target) //we're @ end of list, , if target's not here... success=false; //deletion failed. case delete() returns false. else { //we're between origin , end, , found target prevtptr->nextlnt = nodetptr->nextlnt; delete nodetptr; //delete target. success = true; } } homecoming success; } //******************************************************************** // function name: viewhorse() // function purpose: finds horse struct id target, returns horse reference. // input parameters: // 1. int target - id searched // 2. horse &viewhorse - object of type horse found , returned (by reference) //return value – bool //******************************************************************** //returns reference horse in list id == target //if view() returns false, target not found , &viewhorse not changed viewhorse(). bool list::viewhorse(int target, horse &viewhorse){ bool success = false; listnodeh *nodehptr; nodehptr = headhorse; //start @ origin of list while(nodehptr != null && nodehptr->horse.id < target) //search... nodehptr = nodehptr->nextlnh; //which test failed? if(nodehptr == null || nodehptr->horse.id != target) //we're @ end of list, or didn't find target. success = false; //note: way viewhorse() returns false. else { //nodehptr != null && nodehptr->horse.id == target, found target success = true; viewhorse = nodehptr->horse; //return reference horse in list id == target } homecoming success; }//end list::viewhorse() //******************************************************************** // function name: viewtrans() // function purpose: finds trans struct id target, returns trans reference. // input parameters: // 1. int target - id searched // 2. trans &viewtrans - object of type trans found , returned (by reference) //return value – bool //******************************************************************** //returns reference trans in list id == target //if viewtrans() returns false, target not found , &viewtrans not changed viewtrans(). bool list::viewtrans(int target, trans &viewtrans){ bool success = false; listnodet *nodetptr; nodetptr = headtrans; //start @ origin of list while(nodetptr != null && nodetptr->trans.transhorse.id < target) //search... nodetptr = nodetptr->nextlnt; //which test failed? if(nodetptr == null || nodetptr->trans.transhorse.id != target) //we're @ end of list, or didn't find target. success = false; //note: way viewtrans() returns false. else { //nodetptr != null && nodetptr->horse.id == target, found target success = true; viewtrans = nodetptr->trans; //return reference trans in list id == target } homecoming success; }//end list::viewtrans //******************************************************************** // function name: getnumhorses() // function purpose: returns number of nodes in our list of horses // input parameters: [none] //return value – int //******************************************************************** int list::getnumhorses(){ int horsecounter = 0; listnodeh *nodehptr; nodehptr = headhorse; while(nodehptr != null){ nodehptr = nodehptr->nextlnh; horsecounter++; } homecoming horsecounter; }//end list::getnumhorses() //******************************************************************** // function name: getnumtrans() // function purpose: returns (by reference) numbers of various types of transactions in our list of transactions // input parameters: // 1.int &numa - number of "add" transactions // 2.int &numdel - number of "delete" transactions // 3.int &numdisc - number of "update discipline" transactions // 4.int &nump - number of "update lowbid" transactions //return value – void //******************************************************************** void list::getnumtrans(int &numa, int &numdel, int &numdisc, int &nump){ numa = 0; numdel = 0; numdisc = 0; nump = 0; listnodet *nodetptr; nodetptr = headtrans; while(nodetptr != null){ if(nodetptr->trans.transcode == 'a') numa++; else if(nodetptr->trans.transcode == 'd'){ if (nodetptr->trans.transhorse.disciplinecount == 0) numdel++; else numdisc++; } else if(nodetptr->trans.transcode == 'p'){ nump++; } nodetptr = nodetptr->nextlnt; } }//end list::getnumtrans() //******************************************************************** // function name: transactonce() // function purpose: performs single transaction. // [skip] number of transactions @ origin of linked list of trans skipped. // transaction after skipped transactions attempted. // if transaction fails, skip incremented , function returns false. // in case, thistrans returned reference transaction attempted. // input parameters: // 1. int &skip - number of transactions skip // 2. trans &thistrans - returned transaction attempted // 3. string &failstring - error, if any. //return value – bool //******************************************************************** bool transactonce(int &skip, trans &thistrans, string &failstring){ bool success = true; stringstream failstream; listnodet *nodetptr; nodetptr = headtrans; horse dummyhorse; //for viewhorse() //if 'a', id must not exist in our list of horses //if 'd' or 'p', horse must exist in our list of horses. //if 'd' , disciplinecount != 0, //the existing horse must have disciplinecount <4 //transaction's transhorse.discipline[] must not have duplicates in array //each of transaction's transhorse.discipline[]s must valid (1<= discipline <=4) //find transaction in question (int i=0; < skip; i++){ nodetptr = nodetptr->nextlnt; } //nodetptr pointing appropriate listnodet. if (nodetptr->trans.transcode == 'a' && !list::inserthorse(nodetptr->trans.transhorse)){ failstream << "error horse transaction" << nodetptr->trans.transhorse.id << " via inserthorse()."; success = false; } else if (nodetptr->trans.transcode == 'd'){ //delete or update disciplines if(nodetptr->trans.transhorse.disciplinecount == 0 && !list::deletehorse(nodetptr->trans.transhorse.id)){ //for deletion failstream << "error horse transaction" << nodetptr->trans.transhorse.id << " via deletehorse()."; success = false; } else if(nodetptr->trans.transhorse.disciplinecount != 0 && !list::updatedisc(nodetptr->trans)){ //update disciplines failstream << "error horse transaction" << nodetptr->trans.transhorse.id << " via updatedisc()."; success = false; } } else if (nodetptr->trans.transcode == 'p' && !list::updateprice(nodetptr->trans)){ failstream << "error horse transaction" << nodetptr->trans.transhorse.id << " via updateprice()."; success = false; } else { failstream << "error horse transaction" << nodetptr->trans.transhorse.id << ", not valid transcode."; success = false; } if (success){ //attempt delete transaction if (!list::deletetrans(nodetptr->trans.transhorse.id)){ failstream << "error, not delete transaction " << nodetptr->trans.transhorse.id << "."; success = false; } else //return true , error free failstream << "no error report."; } if (!success) skip++; failstream << "\n"; failstring = failstream.str(); homecoming success; }//end transactonce() //******************************************************************** // function name: updatedisc() // function purpose: "update discipline" transaction, update disciplines of horse in inventory. // input parameters: // 1. trans thistrans - transaction executed //return value – bool //******************************************************************** bool list::updatedisc(trans thistrans){ bool success = true; listnodeh *nodehptr = headhorse; //no need listnodet //obtain pointer horse in question while (nodehptr != null && nodehptr->horse.id < thistrans.transhorse.id) nodehptr = nodehptr->nextlnh; if(nodehptr == null) //failed find horse success = false; //horse found; nodehptr->horse.id == thistrans.transhorse.id else if(thistrans.transcode != 'd') //this isn't "d" transaction... success = false; else if(thistrans.transhorse.disciplinecount == 0) //nothing do, deletion success = false; else if(nodehptr->horse.disciplinecount + thistrans.transhorse.disciplinecount > 4) success = false; //too many disciplines else { //update disciplines while(thistrans.transhorse.disciplinecount !=0){ nodehptr->horse.discipline[nodehptr->horse.disciplinecount] = thistrans.transhorse.discipline[thistrans.transhorse.disciplinecount-1]; nodehptr->horse.disciplinecount++; thistrans.transhorse.disciplinecount--; }//disciplines updated perhaps not sorted //bubble-sort horse's array of disciplines. int i, j; bool swaped = true; int tempint; for(i = 1; (i <= nodehptr->horse.disciplinecount)&&swaped; i++){ swaped = false; (j=0; j< nodehptr->horse.disciplinecount-1; j++){ if(nodehptr->horse.discipline[j] > nodehptr->horse.discipline[j+1]){ tempint = nodehptr->horse.discipline[j]; nodehptr->horse.discipline[j] = nodehptr->horse.discipline[j+1]; nodehptr->horse.discipline[j+1] = tempint; swaped = true; } } } }//end update , sort horse's disciplines homecoming success; }//end list::updatedisc() //******************************************************************** // function name: updateprice() // function purpose: "update lowbid" transaction, update lowbid of horse in inventory. // input parameters: // 1. trans thistrans - transaction executed //return value – bool //******************************************************************** bool list::updateprice(trans thistrans){ bool success = true; listnodeh *nodehptr; nodehptr = headhorse; //obtain pointer horse in question while (nodehptr != null && nodehptr->horse.id < thistrans.transhorse.id) nodehptr = nodehptr->nextlnh; if(nodehptr == null) //failed find horse success = false; //else, horse found; nodehptr->horse.id == thistrans.transhorse.id else if(thistrans.transcode != 'p') //this isn't "p" transaction... success = false; else { //update lowbid nodehptr->horse.lowbid = thistrans.transhorse.lowbid; } homecoming success; }//end list::updateprice() //******************************************************************** // function name: reportonetransaction() // function purpose: prints report.out single transaction. called transact(). // input parameters: // 1. fstream &strm - stream print to. // 2. trans onetrans - transaction beingness reported. //return value – void //******************************************************************** void list::reportonetransaction(fstream &strm, trans onetrans){ //some "ragged arrays" string breeddescription[maxbreeds+1] = {"dummy", "pleasure saddle horse", "american fox trotter", "virginia highlander", "arabian"};//21 string disciplinedescription[maxdisciplines+1] = {"dummy", "dressage", "jumper", "hunter", "western pleasure"};//16 if (onetrans.transcode == 'a') strm << "horse added inventory named '" ; else strm << "horse removed inventory named '" ; strm << onetrans.transhorse.name << "'"<< endl; strm << "______________________________________________________________________\n";//70 strm <<"id height age m/f lowbid "<< onetrans.transhorse.disciplinecount << " discipline(s): breed\n"; strm << left << setw(3) <<onetrans.transhorse.id <<" ";//id strm << left << setw(5) <<setprecision(2) <<onetrans.transhorse.height <<" ";//height (double) strm << left << setw(3) <<onetrans.transhorse.age <<" ";//age strm << left << setw(1) <<onetrans.transhorse.gendercode; //gendercode (char) strm << " "<<left<<'$'; strm << right << fixed << setw(8) << setprecision(2) << onetrans.transhorse.lowbid ;//lowbid (double) strm << left << " " << setw(16) << disciplinedescription[onetrans.transhorse.discipline[0]] ; strm << " " << breeddescription[onetrans.transhorse.breed] << endl; (int j=1; j < onetrans.transhorse.disciplinecount; j++){ strm << " "; strm << disciplinedescription[onetrans.transhorse.discipline[j]] << endl; } strm << endl << endl; }//end reportonetransaction()
you forgot list::
on transactonce
it's trying parse stand-alone function. did myself couple days ago...
c++ list linked-list g++
Comments
Post a Comment