Assertion Error and Ostream problem
Oct 15, 2011 at 6:02am UTC
I'm having two problems with my code.
First problem: I'm trying to get it to print correctly, but I can't figure out the best way to call the Winery class' << overload.
Here's the printing code in Winery.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void Winery::displayColumnHeadings(ostream& out)
{
// print out column headings for lists of wineries, as specified by lab1output.txt
out << setw(26) << "name" << setw(19) << "location" << setw(6) << "acres" << setw(6)
<< "rating" << endl << "---- -------- ----- ------" << endl;
}
ostream& operator <<(ostream& out, Winery *w)
{
// print out a winery, as specified by lab1output.txt
out << setw(26) << w->name << setw(19) << w->location << setw(6) << w->acres << setw(6)
<< w->rating << endl;
return out;
}
This is how I'm calling it:
1 2 3 4 5 6 7 8 9 10
void List::displayByName(ostream& out) const
{
Node* curr;
Winery* winptr;
winptr = & ;
for (curr = headByName; curr; curr = curr->nextByName)
out << '\t' << curr->item << endl;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Second problem: The program compiles without errors in VisualStudio, but the program won't run all the way through without an assertion error. I've been trying to figure this out for a while, but I have no idea what this even means. Will somebody please help?
Here's the function that I think may be the culprit:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
void List::insert(const Winery& winery)
{
Node * prev = NULL;
Node * curr = headByName;
//traverse to find the position to insert
while (curr!=NULL && curr->item.getName() < winery.getName())
{
prev = curr;
curr = curr->nextByName;
}
//the data already exists
if (curr && curr->item.getName() == winery.getName());
//return false;
//insert the data here
else
{
//create new node to contain the data
Node * newNode = new Node(winery);
newNode->item = winery;
newNode->nextByName = NULL;
newNode->nextByRating = NULL;
//link the newNode into the linked list
newNode->nextByName = curr;
if (prev == NULL)
headByName = newNode;
else
prev->nextByName = newNode;
//size++;
//return true;
curr = headByRating;
while (curr!=NULL && curr->item.getRating() < winery.getRating())
{
prev = curr;
curr = curr->nextByRating;
}
//link the newNode into the linked list
newNode->nextByRating = curr;
if (prev == NULL)
headByRating = newNode;
else
prev->nextByRating = newNode;
}
}
Here's list.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
class List
{
public :
List(void ); // constructor
virtual ~List(void ); // destructor
// Print out the wineries in alphabetical order by name,
// by calling winery's operator<< for each winery.
void displayByName(ostream& out) const ;
void displayByRating(ostream& out) const ;
void insert(const Winery& winery);
Winery * const find(const char * const name) const ;
bool remove(const char * const name);
private :
// defines each node in the doubly-threaded linked list.
struct Node
{
Node(const Winery& winery); // constructor
Winery item;
Node *nextByName;
Node *nextByRating;
};
Node *headByName;
Node *headByRating;
};
Thank you!
Topic archived. No new replies allowed.