Hi all,
I am trying to return a linked list from a function. The code compiles, but chokes when I try to use the function getEquation(). Assuming returning a link list is the problem, how do I rectify this?
PHP Code:
/*
* eq.h
*
* Created by Jeffery Shipman on 1/19/06.
* class of equations accepting polys
*
* poly header must load first
*/
class eq
{
public:
eq(string n); //default constructor
eq(const eq &); //copy constructor
void add(poly *); //add poly to the equation
void show(); //display
void setName(string n){name = n;} //set name of equation
string getName() const;//returns name of equation
list <poly> getEquation() const; //returns equation
void eq_sum (eq &); //produce sum of two equations
void simplify(); //sum like elements in a single list
bool operator==(const eq &) const; //compare equal
eq operator=( const eq & ); //assign lists
private:
string name;
list <poly> equation;
};
eq::eq(string n)
{
name = n;
list <poly>::iterator itr;
itr = equation.begin();
}
eq::eq(const eq &e)
{
poly *element;
name = e.name;
list <poly>::const_iterator const_itr;
const_itr = equation.begin();
while(const_itr!=equation.end())
{
element = new poly(const_itr->getCoeff (), const_itr->getExp ());
add(element);
const_itr++;
}
}
void eq::add(poly *element)
{
//p is pointing at a poly
//push_front *p follows the pointer to
//that poly..and pushes him/her to
//the equation
equation.push_back(*element);
}
void eq::show()
{
cout<<"polys currently in equation" << endl;
list <poly>::iterator itr;
itr = equation.begin();
while(itr!=equation.end())
{
if (itr->getCoeff()!=0)
{
itr->display();
}
itr++;
}
}
string eq::getName() const
{
return name;
}
list <poly> eq::getEquation() const
{
return equation;
}
void eq::simplify()
{
list <poly>::iterator itr;
list <poly>::iterator itr2;
itr = equation.begin();
itr2 = equation.begin();
itr2++;
//cout << "while(p!=equation.end() && itr!=itr2)" << endl;
while(itr!=equation.end() && itr!=itr2)
{
//cout << " while(itr2!=equation.end() && itr!=itr2)" << endl;
while(itr2!=equation.end() && itr!=itr2)
{
//cout << " " ;
//itr->display();
//cout << " == " ;
//itr2->display() ;
//cout << endl;
if (itr->getExp() == itr2->getExp())
{
itr->setCoeff(itr->getCoeff() + itr2->getCoeff()); //add the coeff
equation.erase(itr2); //erase the match from the list
//the following line of code are not necessary in gcc 4
//but are in gcc 3
//iterator pointing no where after erase
//assign iterator
itr2 = itr;
}
itr2++;
}
//the following three lines of code are not necessary in gcc 4
//but are in gcc 3
itr++;
itr2 = itr;
itr2++;
}
}
void eq::eq_sum (eq &second)
{
list <poly>::iterator itr;
list <poly>::iterator itr2;
itr = equation.begin();
itr2 = second.getEquation().begin();
//simply two given equations
simplify();
show();
cout << endl << endl;
second.simplify();
second.show();
cout << endl << endl;
int test = 0;
poly *element;
eq sum ("sum");
cout <<"after creation of sum" << endl << endl;
while (itr != equation.end())
{
cout <<" inside first while loop" << endl;
test = 0;
itr2 = second.getEquation().begin();
while (itr2 != second.getEquation().end())
{
cout << " ";
itr->display();
cout << " == " ;
itr2->display();
cout << endl;
//combine similar elements then add to finally equation
if (itr->getExp() == itr2->getExp())
{
element = new poly(itr->getCoeff() + itr2->getCoeff(),
itr->getExp() + itr2->getExp());
sum.add(element);
test++;
}
itr2++;
}
//if there are no matches add element to sum
if (test == 0)
{
element = new poly(itr->getCoeff(), itr->getExp());
sum.add(element);
}
itr++;
}
//re-initialize values for new looping
itr = equation.begin();
itr2 = second.getEquation().begin();
while (itr2 != second.getEquation().end())
{
test = 0;
itr = equation.begin();
while (itr != getEquation().end())
{
if (itr2->getExp() == itr->getExp())
{
test++;
}
itr++;
}
if (test == 0)
{
element = new poly(itr2->getCoeff(), itr2->getExp());
sum.add(element);
}
itr2++;
}
sum.show();
}
bool eq::operator==(const eq &right) const //compare equal
{
list <poly>::const_iterator itr;
list <poly>::const_iterator itr2;
itr = equation.begin();
itr2 = right.equation.begin();
if (equation.size () == right.equation.size ())
{
while(itr!=equation.end())
{
if (!((itr->getCoeff() == itr2->getCoeff()) && (itr->getExp() == itr2->getExp())) )
return false;
else
{
itr++;
itr2++;
}
}
return true;
}
else
return false;
}
eq eq::operator=( const eq &right )
{
poly *element;
list <poly>::const_iterator itr;
list <poly>::const_iterator itr2;
itr = equation.begin();
itr2 = right.equation.begin();
if (this != &right)
{
//cout << "pass the first if" << endl;
//clear target list
while(itr!=equation.end())
{
//cout << " inside the clear target while" << endl;
equation.pop_front ();
}
//cout << "pass the clear target while" << endl;
//cout << "size of right equation " << right.equation.size() << endl;
while(itr2!=right.equation.end())
{
//cout << " inside the add while" << endl;
element = new poly(itr2->getCoeff (), itr2->getExp ());
add(element);
itr2++;
}
}
return *this;
}