Для назначения я должен сделать обобщенный связанный список, используя массив. Весь код работает нормально и был протестирован. Однако, когда я пытаюсь реализовать перегруженный оператор для <<, я получаю тонны ошибок, которые я действительно не понимаю, основной из которых является «определение оператора явной специализации» << «в объявлении друга». </p>
Вот мой код:
#include <iostream>
using namespace std;
//************************************************************************************
// GLROW CLASS
//************************************************************************************
template <class DT>
class GLRow; //class prototype
template <class DT>
class GLRow {
protected:
DT* _Info;
int _Next;
int _Down;
public:
GLRow ();
GLRow (const DT& newInfo);
GLRow (const GLRow<DT>& anotherOne);
GLRow<DT>& operator= (const GLRow<DT>& anotherOne); //Make sure you do a$
int getNext();
int getDown();
DT& getInfo() const;
int setNext(int n);
int setDown(int d);
int setInfo (const DT& x);
~GLRow(); //destructor
friend ostream &operator <<(ostream &s, GLRow<DT> &oneGLRow)
{
cout<<oneGLRow.getInfo();
return s;
}
};
//************************************************************************************
// ARRAYGLL CLASS
//************************************************************************************
template <class DT>
class ArrayGLL; //class prototype
template <class DT>
class ArrayGLL {
protected:
GLRow<DT>** myGLL;
int maxSize; //Maximum size of the array $
int firstElement;
int firstFree;
public:
ArrayGLL ();
ArrayGLL (int size);
ArrayGLL (ArrayGLL<DT>& anotherOne);
ArrayGLL<DT>& operator= (ArrayGLL<DT>& anotherOne);
void display (); //display in parenthesis for$
int find (const DT& key); //return the index p$
void findDisplayPath (const DT& Key); // as you travel thr$
int noFree (); //return the number of free $
int size (); //return the number of eleme$
int parentPos(const DT& Key); // provide the locat$
GLRow<DT>& operator [] (int pos); //return the GLRow that is i$
int getFirstFree();
int getFirstElement();
void setFirstFree (int pos);
void setFirstElement (int pos);
~ArrayGLL (); //destructor
friend ostream& operator<< <DT>(ostream& s, ArrayGLL<DT>& OneGLL)
{
for(int i = 0; i<oneGLL.size();i++)
{
cout<< oneGLL[i]->getInfo()<<endl;
}
return s;
}
};
//************************************************************************************
// GLROW CLASS DEFINITIONS
//************************************************************************************
ostream& operator <<(ostream& s, ArrayGLL<DT>& oneGLL)
{
}
//------------------------------------------------------------------------------------------
// CONSTRUCTOR
//------------------------------------------------------------------------------------------
template <class DT>
GLRow<DT>::GLRow()
{
_Info = nullptr;
_Next = -1;
_Down = -1;
}
//------------------------------------------------------------------------------------------
// CONSTRUCTOR
//------------------------------------------------------------------------------------------
template <class DT>
GLRow<DT>::GLRow(const DT &newInfo)
{
_Info = new DT(newInfo);
_Next = -1;
_Down = -1;
}
//------------------------------------------------------------------------------------------
// = COPY CONSTRUCTOR
//------------------------------------------------------------------------------------------
template <class DT>
GLRow<DT> & GLRow<DT>::operator=(const GLRow<DT> &anotherOne)
{
_Info = anotherOne._Info;
_Next = anotherOne._Next;
_Down = anotherOne._Down;
}
//------------------------------------------------------------------------------------------
// GETNEXT
//------------------------------------------------------------------------------------------
template <class DT>
int GLRow<DT>::getNext()
{
return _Next;
}
//------------------------------------------------------------------------------------------
// GETDOWN
//------------------------------------------------------------------------------------------
template <class DT>
int GLRow<DT>::getDown()
{
return _Down;
}
//------------------------------------------------------------------------------------------
// GET INFO
//------------------------------------------------------------------------------------------
template <class DT>
DT & GLRow<DT>::getInfo() const
{
return *_Info;
}
//------------------------------------------------------------------------------------------
// SET NEXT
//------------------------------------------------------------------------------------------
template <class DT>
int GLRow<DT>::setNext(int n)
{
_Next = n;
}
//------------------------------------------------------------------------------------------
// SET DOWN
//------------------------------------------------------------------------------------------
template <class DT>
int GLRow<DT>::setDown(int d)
{
_Down =d;
}
//------------------------------------------------------------------------------------------
// SET INFO
//------------------------------------------------------------------------------------------
template <class DT>
int GLRow<DT>::setInfo(const DT &x)
{
_Info = new DT(x);
}
//------------------------------------------------------------------------------------------
// DESTRCUTOR
//------------------------------------------------------------------------------------------
template <class DT>
GLRow<DT>::~GLRow()
{
_Next = -1;
_Down = -1;
delete _Info;
}
//************************************************************************************
// ARRAYGL CLASS DEFINITIONS
//************************************************************************************
//------------------------------------------------------------------------------------------
// CONSTRUCTOR
//------------------------------------------------------------------------------------------
template <class DT>
ArrayGLL<DT>::ArrayGLL()
{
maxSize = 10;
firstElement = 0;
firstFree = 0;
myGLL = new GLRow<DT>*[10];
for(int i=0; i<maxSize;i++)
{
myGLL[i] = new GLRow<DT>(999);
}
}
//------------------------------------------------------------------------------------------
// DESTRUCTOR
//------------------------------------------------------------------------------------------
template <class DT>
ArrayGLL<DT>::~ArrayGLL()
{
maxSize = 0;
firstElement = 0;
firstFree = 0;
delete myGLL;
}
//------------------------------------------------------------------------------------------
// CONTRUCTOR
//------------------------------------------------------------------------------------------
template <class DT>
ArrayGLL<DT>::ArrayGLL(int size)
{
maxSize = size;
firstElement = 0;
firstFree = 0;
myGLL = new GLRow<DT>*[size];
for(int i=0; i<size;i++)
{
myGLL[i] = new GLRow<DT>(999);
}
}
//------------------------------------------------------------------------------------------
// [] OPERATOR
//------------------------------------------------------------------------------------------
template <class DT>
GLRow<DT> & ArrayGLL<DT>::operator[](int pos)
{
return *myGLL[pos];
}
//------------------------------------------------------------------------------------------
// GETFIRSTFREE
//------------------------------------------------------------------------------------------
template <class DT>
int ArrayGLL<DT>::getFirstFree()
{
return firstFree;
}
//------------------------------------------------------------------------------------------
// GET FIRSTELEMENT
//------------------------------------------------------------------------------------------
template <class DT>
int ArrayGLL<DT>::getFirstElement()
{
return firstElement;
}
//------------------------------------------------------------------------------------------
// SET FIRSTFREE
//------------------------------------------------------------------------------------------
template <class DT>
void ArrayGLL<DT>::setFirstFree(int pos)
{
firstFree = pos;
}
//------------------------------------------------------------------------------------------
// SET FIRST ELEMENT
//------------------------------------------------------------------------------------------
template <class DT>
void ArrayGLL<DT>::setFirstElement(int pos)
{
firstElement = pos;
}
//------------------------------------------------------------------------------------------
// COPY CONSTRUCTOR
//------------------------------------------------------------------------------------------
template <class DT>
ArrayGLL<DT>::ArrayGLL( ArrayGLL<DT> & anotherOne)
{
maxSize = anotherOne.size();
firstElement = 0;
firstFree = 0;
myGLL = new GLRow<DT>*[anotherOne.size()];
for(int i =0;i<anotherOne.size();i++)
{
myGLL[i] = new GLRow<DT>(anotherOne[i]);
}
}
//------------------------------------------------------------------------------------------
// SIZE
//------------------------------------------------------------------------------------------
template <class DT>
int ArrayGLL<DT>::size()
{
return maxSize;
}
//------------------------------------------------------------------------------------------
// = OPERATOR
//------------------------------------------------------------------------------------------
template <class DT>
ArrayGLL<DT> & ArrayGLL<DT>::operator=( ArrayGLL<DT> & anotherOne)
{
maxSize = anotherOne.size();
firstElement = 0;
firstFree = 0;
myGLL = new GLRow<DT>*[anotherOne.size()];
for(int i =0;i<anotherOne.size();i++)
{
myGLL[i] = new GLRow<DT>(anotherOne[i]);
}
}
//------------------------------------------------------------------------------------------
// FIND
//------------------------------------------------------------------------------------------
template <class DT>
int ArrayGLL<DT>::find(const DT &key)
{
for(int i=0;i<maxSize;i++)
{
if(myGLL[i]->getInfo()==key)
{
return i;
}
}
return -1;
}
//------------------------------------------------------------------------------------------
// PARENT POSITION
//------------------------------------------------------------------------------------------
template <class DT>
int ArrayGLL<DT>::parentPos(const DT &key)
{
int a;
int b;
int c;
for(int i =0; i<maxSize;i++)
{
if(myGLL[i]->getDown()!=-1)
{
if(myGLL[myGLL[i]->getDown()]->getInfo()==key)
{
return i;
}
if(myGLL[myGLL[i]->getDown()]->getNext()!=-1)
{
if(myGLL[myGLL[myGLL[i]->getDown()]->getNext()]->getInfo()==key)
{
return i;
}
}
if(myGLL[myGLL[myGLL[i]->getDown()]->getNext()]->getNext()!=-1)
{
if(myGLL[myGLL[myGLL[myGLL[i]->getDown()]->getNext()]->getNext()]->getInfo()$
{
return i;
}
}
}
}
}
return -1;
}
//------------------------------------------------------------------------------------------
// DISPLAY
//------------------------------------------------------------------------------------------
template <class DT>
void ArrayGLL<DT>::display()
{
cout<<"INDEX"<<" "<<"INFO"<<" "<<"NEXT"<<" "<<"DOWN"<<endl;
for(int i=0;i<maxSize;i++)
{
cout<<i<<" "<< myGLL[i]->getInfo()<<" "<< myGLL[i]->getNext()<<" "<< myGLL[i]$
}
}
//------------------------------------------------------------------------------------------
// NOFREE
//------------------------------------------------------------------------------------------
template <class DT>
int ArrayGLL<DT>::noFree()
{
int NumFree =0;
for(int i =0; i<maxSize;i++)
{
if(myGLL[i]->getInfo()==999)
NumFree++;
}
return NumFree;
}
//------------------------------------------------------------------------------------------
// FIND DISPLAY PATH
//------------------------------------------------------------------------------------------
template <class DT>
void ArrayGLL<DT>::findDisplayPath(const DT &Key)
{
DT i = Key;
int x;
while(x!=-1)
{
x = parentPos(i);
if(x!=-1)
cout<< myGLL[x]->getInfo()<<endl;
if(x!=-1)
i = myGLL[x]->getInfo();
}
}
Любое понимание было бы здорово, у меня нет большого опыта работы с ostream и тому подобным.