D
Der Andere
My assignment is to derive several container classes from class Node. When I
try to compile the following program, the compiler gives the following:
error C2440: '=' : cannot convert from 'class Node<int,1> *' to 'class
SLNode<int> *'
The error relates to the expression temp=temp->succ[0] in the function
writePath() and I do not know what to do about it.
#include <iostream>
using namespace std;
template<typename Info, int Arity>
class Node
{
protected:
Info info;
Node *succ[Arity];
public:
Node (const Info& i) : info(i)
{for (int j = 0; j < Arity; j++)
succ[j] = NULL;
};
};
// ------- Singly linked list -------
// ----------------------------------
template<typename Info>
class SLNode : public Node<Info,1>
{
public:
SLNode<Info> (const Info& i) : Node<Info,1>(i) {}
void append (const Info& i);
bool contains (const Info& i);
SLNode remove(const Info& i);
void writePath() // Später entfernen
{
for (SLNode *temp=this; temp->succ!=NULL; temp = temp->succ[0])
cout << info << " -> ";
cout << endl;
}
};
template<typename Info>
void SLNode<Info> :: append (const Info& i)
{
for (SLNode *temp=this; temp->succ!=NULL; temp=temp->succ);
temp->succ = new SLNode(i);
};
template<typename Info>
bool SLNode<Info> :: contains(const Info &i)
{
for (SLNode *temp=this; temp!=NULL; temp=temp->succ)
if (temp->info==i) return true;
return false;
}
template<typename Info>
SLNode<Info> SLNode<Info> :: *remove(const Info &i)
{
SLNode *before,*after;
if (i==*this) // Calling object has to be deleted
{
after = this->succ;
delete this;
return after;
}
for (*before=this; before->succ!=&i; before=before->succ)
if (before==NULL) return this; // Element does not exist
*after = (before->succ)->succ;
delete before->succ;
before->succ = after;
return this;
}
void main()
{
int nr=0;
SLNode<int> *SLNode1 = new SLNode<int>(10);
SLNode1->writePath();
// for (int i=1; i<10; i++)
// SLNode1.append(i);
}
BTW, is there an easier way to instantiate SLNode than the way I did it (it
really looks awkward)? I would prefer to have an instance of SLNode<int>
initialized with a certain value (say 10), not merely a pointer.
I would like SLNode to have the member variable succ instead of a somewhat
non-sensical succ[1]. Is the only way to achieve this to declare a new
member variable succ?
Thanks.
Regards,
Matthias
try to compile the following program, the compiler gives the following:
error C2440: '=' : cannot convert from 'class Node<int,1> *' to 'class
SLNode<int> *'
The error relates to the expression temp=temp->succ[0] in the function
writePath() and I do not know what to do about it.
#include <iostream>
using namespace std;
template<typename Info, int Arity>
class Node
{
protected:
Info info;
Node *succ[Arity];
public:
Node (const Info& i) : info(i)
{for (int j = 0; j < Arity; j++)
succ[j] = NULL;
};
};
// ------- Singly linked list -------
// ----------------------------------
template<typename Info>
class SLNode : public Node<Info,1>
{
public:
SLNode<Info> (const Info& i) : Node<Info,1>(i) {}
void append (const Info& i);
bool contains (const Info& i);
SLNode remove(const Info& i);
void writePath() // Später entfernen
{
for (SLNode *temp=this; temp->succ!=NULL; temp = temp->succ[0])
cout << info << " -> ";
cout << endl;
}
};
template<typename Info>
void SLNode<Info> :: append (const Info& i)
{
for (SLNode *temp=this; temp->succ!=NULL; temp=temp->succ);
temp->succ = new SLNode(i);
};
template<typename Info>
bool SLNode<Info> :: contains(const Info &i)
{
for (SLNode *temp=this; temp!=NULL; temp=temp->succ)
if (temp->info==i) return true;
return false;
}
template<typename Info>
SLNode<Info> SLNode<Info> :: *remove(const Info &i)
{
SLNode *before,*after;
if (i==*this) // Calling object has to be deleted
{
after = this->succ;
delete this;
return after;
}
for (*before=this; before->succ!=&i; before=before->succ)
if (before==NULL) return this; // Element does not exist
*after = (before->succ)->succ;
delete before->succ;
before->succ = after;
return this;
}
void main()
{
int nr=0;
SLNode<int> *SLNode1 = new SLNode<int>(10);
SLNode1->writePath();
// for (int i=1; i<10; i++)
// SLNode1.append(i);
}
BTW, is there an easier way to instantiate SLNode than the way I did it (it
really looks awkward)? I would prefer to have an instance of SLNode<int>
initialized with a certain value (say 10), not merely a pointer.
I would like SLNode to have the member variable succ instead of a somewhat
non-sensical succ[1]. Is the only way to achieve this to declare a new
member variable succ?
Thanks.
Regards,
Matthias