For practising , I wrote a linked list inspired from a C++ book. But when I use the function Iterate there is an error message because of 2 undefined variables : itsNext and itsRect from the function getRect in class Rect_Node . If I remove Iterate , the rectangles that I created are correctly displayed .
Perhaps someone knows why this is happening , here is the code .
Code:
#include <iostream>
using namespace std;
class Rectangles
{
public:
Rectangles(){};
Rectangles(int L): Length(L) {Height = Length / 2;}
virtual~Rectangles(){};
virtual void Display()const = 0;
int getLength()const{return Length;}
private:
int Length;
int Height;
};
void Rectangles::Display()const
{
for(int i = 0; i < Height; i++)
{
for(int j = 0; j < Length; j++)
cout << " * ";
cout << "\n";
}
}
class Number_Rect : public Rectangles
{
public:
Number_Rect(): itsNB(01), Rectangles(12){};
Number_Rect(int NB, int L): itsNB(NB), Rectangles(L) {}
void Display()const
{ Rectangles::Display(); cout << " Number = "<< itsNB << endl << endl;}
private:
int itsNB;
};
class Char_Rect : public Rectangles
{
public:
Char_Rect(): itsChar('a'), Rectangles(12){};
Char_Rect(char C, int L): itsChar(C), Rectangles(L) {}
void Display()const
{ Rectangles::Display(); cout << " Character = "<< itsChar << endl << endl;}
private:
char itsChar;
};
class Rect_Node
{
public:
Rect_Node(Rectangles* pRectangle): itsRect(pRectangle), itsNext(0) {};
~Rect_Node(){};
Rectangles* getRect()const{if(itsRect)return itsRect; else return NULL;}
void setNext(Rect_Node* Next){itsNext = Next;}
Rect_Node* getNext()const{return itsNext;}
private:
Rect_Node* itsNext;
Rectangles* itsRect;
};
class Rect_List
{
public:
Rect_List(): pHead(0), itsCount(0){};
~Rect_List(){delete pHead;}
Rectangles* Find(int Position, int Rectangle_Number)const;
int getCount()const{return itsCount;}
Rectangles* GetFirst()const;
void Insert(Rectangles*);
void Iterate()const;
Rectangles* operator[](int)const;
private:
Rect_Node* pHead;
int itsCount;
};
Rectangles* Rect_List :: GetFirst()const
{
if(pHead)
return pHead->getRect();
else return NULL;
}
Rectangles* Rect_List :: operator[](int Offset)const
{
Rect_Node* Temp = pHead;
if (!pHead)
return NULL;
if (Offset > itsCount)
return NULL;
for(int i = 0; i < Offset; i++)
Temp = Temp->getNext();
return Temp->getRect();
}
Rectangles* Rect_List :: Find(int Position, int Rectangle_Number)const
{
Rect_Node* Temp = 0;
for(Temp = pHead, Position = 0; Temp != NULL; Temp->getNext(), Position++)
{
if (Temp->getRect()->getLength() == Rectangle_Number)
break;
}
if(Temp == NULL)
return NULL;
else
return Temp->getRect();
}
void Rect_List :: Iterate()const
{
Rect_Node* Temp = pHead;
/*do
{
Temp->getRect()->Display();
}while (Temp = Temp->getNext());*/
for( int i = 0; i < itsCount; i++ )
{
Temp->getRect()->Display();
Temp = Temp->getNext();
cout << " debug " << i << endl;
}
cout << endl << itsCount << endl;
}
void Rect_List :: Insert(Rectangles* pRectangle)
{
int NBL = 10;
NBL = pRectangle->getLength();
int testNB = 10;
Rect_Node* newRect = new Rect_Node(pRectangle);
Rect_Node* Temp = 0;
Rect_Node* Temp1 = 0;
itsCount++;
Temp = pHead;
if(!pHead)
{
pHead = newRect;
newRect->setNext(0);
pHead->getRect()->Display();
return;
}
if(pHead->getRect()->getLength() > NBL)
{
newRect->setNext(pHead);
pHead = Temp;
pHead->setNext(0);
newRect->getRect()->Display();
return;
}
for(;;)
{
if(!Temp->getNext())
{
pHead->setNext(newRect);
newRect->setNext(0);
newRect->getRect()->Display();
return;
}
Temp1 = Temp->getNext();
testNB = Temp1->getRect()->getLength();
if( testNB > NBL )
{
Temp->setNext(newRect);
newRect->setNext(Temp1);
Temp1->setNext(0);
newRect->getRect()->Display();
return;
}
Temp = Temp1;
}
}
int main()
{
Rect_List r1;
Rectangles* pRectangle = 0;
pRectangle = new Number_Rect(1, 2);
r1.Insert(pRectangle);Rectangles* pRectangle2 = 0;
pRectangle2 = new Number_Rect(2, 8);
r1.Insert(pRectangle2);Rectangles* pRectangle3 = 0;
pRectangle3 = new Number_Rect(3, 6);
r1.Insert(pRectangle3);Rectangles* pRectangle4 = 0;
pRectangle4 = new Number_Rect(4, 10);
r1.Insert(pRectangle4);
cout << endl;
r1.Iterate();
/*int RectLength;
int value1;
char value2;
int choice = 99;
while(choice != 0)
{
cout << " (0) Quit, (1) Number_Rect, (2) Character_Rect :";
cin >> choice;
if (choice != 0)
{
cout << " \nLength? : ";
cin >> RectLength;
if (choice == 1)
{
cout << " \nRect Number?";
cin >> value1;
pRectangle = new Number_Rect(value1, RectLength);
}
else
{
cout << " \nRect Character?";
cin >> value2;
pRectangle = new Char_Rect(value2, RectLength);
}
r1.Insert(pRectangle);
(r1.GetFirst())->Display();
cout << " itsCount = " << r1.getCount()<<endl;
}// end if
}// end while
r1.Iterate();
(r1.GetFirst())->Display();*/
return 0;
}
Thanks .