Okay, so I've been working on an assignment for a few days, it is a doubly list and I seem to be getting an infinite loop in my program. I believe the cause to be in my insert_after function (lines 149-151). Debugging used gdb is telling me that nextfield is pointing to the iterator spot but that should be previousfield I believe as it is in the the construction of my node class but I can't seem to figure out how to fix this loop.
#include <fstream>
#include "dlist.h"
#include "swatch.h"
#include "node_iterator.h"
usingnamespace std;
int main()
{
dlist<Swatch> swatches;
dlist<Swatch>::iterator it;
ifstream fin;
fin.open("swatches.txt");
if (fin.fail())
{
cout << "Could not open input file." << endl;
return 1;
}
Swatch tmp;
while (fin >> tmp)
{
int red = tmp.get_red();
int green = tmp.get_green();
int blue = tmp.get_blue();
if ( (red>=green) && (red>=blue) ) // red is dominant
{
swatches.front_insert(tmp);
}
elseif ( (green >= red) && (green >= blue) )
// green is dominant
{
swatches.rear_insert(tmp);
}
else // blue is dominant
{
it = swatches.begin();
for(int i = 0;i < swatches.size()/2;i++)
++it; // loop moves iterator to the middle
if(swatches.size()%2 == 1){
if(swatches.size()>2){
it++;
}
swatches.insert_before(it,tmp);
}
else{
swatches.insert_after(it,tmp);
}
}
}
fin.close();
dlist<Swatch> copy(swatches); // make a copy
// remove the front, back, and centermost swatch from the copy
copy.front_remove();
copy.rear_remove();
it = copy.begin();
for(int i =0; i < copy.size()/2; ++i)
++it;
if(copy.size()%2 ==1) ++it; // if list has a true middle
// step up into it
copy.remove(it);
// output the original list frontwards
for (dlist<Swatch>::iterator i=swatches.begin(); i != swatches.end(); ++i)
{
cout << *i << endl;
}
cout << endl << endl; // some space
// output the copy frontwards
for (dlist<Swatch>::iterator i=copy.begin(); i != copy.end(); ++i)
{
cout << *i << endl;
}
cout << endl << endl; // some space
// output the original backwards
for (dlist<Swatch>::iterator i=swatches.r_begin(); i != swatches.r_end(); --i)
{
cout << *i << endl;
}
cout << endl << endl; // some space
// destroy the original list by alternating between removal of first and
// last items. Print each item as it is removed
int counter=0;
while (swatches.size() > 1)//0
{
if (swatches.begin() != NULL)
cout<<*swatches.begin()<<endl;
swatches.front_remove();
if(swatches.r_begin() != NULL)//if(swatches.size() > 0){
cout<<*swatches.r_begin()<<endl;
swatches.rear_remove();
}
cout << endl << endl; // some space
// output the copy backwards
for (dlist<Swatch>::iterator i=copy.r_begin(); i != copy.r_end(); --i)
{
cout << *i << endl;
}
return 0;
}
the problem is in while (fin >> tmp)
The ifstream operator >> has no way of knowing how to read class from text.
You need to overload the >> oprator or input individual members of the class using >>
If the member itself is a class then you will need to input the members of that class using >> (or obviously overload >> for that class too)