473,388 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Problem with templates and inheritance

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

--
Für emails Anweisung in der Adresse befolgen
Jul 22 '05 #1
3 1272
Der Andere wrote:
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])
temp is of type SLNode<Info>* and temp->succ[0] is of type
Node<Info,1>*, which is the base class. You cannot implicitly convert
from pointer to a base class into a pointer to derived because such a
conversion is considered unsafe. You have to use a cast, like:

for (SLNode *temp=this; temp->succ!=NULL;
temp = static_cast<SLNode*>(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)
This must be:

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) ^
leave out that *.
if (before==NULL) return this; // Element does not exist
*after = (before->succ)->succ;
delete before->succ;
before->succ = after;
return this;
}

void main()
main must return int in C++.
{
int nr=0;
SLNode<int> *SLNode1 = new SLNode<int>(10);
SLNode1->writePath();
// for (int i=1; i<10; i++)
// SLNode1.append(i);
Your object is not properly destroyed. For every use of new, there
should be a delete. So add:

delete SLNode1;
}
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.
Uhm, it's you who chose to use a pointer and dynamic allocation. If you
don't want that, then just don't do it. Write:

SLNode<int> SLNode1(10);
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?


You could also write *succ instead of succ[0], but that's not much
better.

Jul 22 '05 #2
"Der Andere" <ma**************@gmx.net> wrote in message news:<c6*************@news.t-online.com>...
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.


Correct. The templates don't help readability, so I'll strip the example
down. You could have done that yourself, BTW.

class Base {
protected:
Base* succ;
}
class Derived {
void foo() {
Derived* temp = this->succ;
}
}
This fails, because this->succ might point to AnotherDerived object,
instead of the expected Derived object. Base::succ doesn't have the
correct type.

The hack is to use dynamic_cast< >, which probably means that
Base::~Base must be virtual (or another function, you need RTTI).
In this case, the Base*->Derived* conversion check is delayed to
runtime.

Th neater solution is the CRTP pattern by Coplien:

template <typename Derived> class Base
{
protected:
Derived* succ;
}
class Derived : public Base<Derived> {
//...
};

Regards,
Michiel Salters
Jul 22 '05 #3
> > 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])


temp is of type SLNode<Info>* and temp->succ[0] is of type
Node<Info,1>*, which is the base class. You cannot implicitly convert
from pointer to a base class into a pointer to derived because such a
conversion is considered unsafe. You have to use a cast, like:

for (SLNode *temp=this; temp->succ!=NULL;
temp = static_cast<SLNode*>(temp->succ[0]))


Looks really ugly ;-) (though perfectly understandable)
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)


This must be:

SLNode<Info>* SLNode<Info> :: remove(const Info &i)


Oops.
{
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)

^
leave out that *.
if (before==NULL) return this; // Element does not exist
*after = (before->succ)->succ;
delete before->succ;
before->succ = after;
return this;
}

void main()


main must return int in C++.
{
int nr=0;
SLNode<int> *SLNode1 = new SLNode<int>(10);
SLNode1->writePath();
// for (int i=1; i<10; i++)
// SLNode1.append(i);


Your object is not properly destroyed. For every use of new, there
should be a delete. So add:

delete SLNode1;
}
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.


Uhm, it's you who chose to use a pointer and dynamic allocation. If you
don't want that, then just don't do it. Write:

SLNode<int> SLNode1(10);
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?


You could also write *succ instead of succ[0], but that's not much
better.


Thanks.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Markus Seeger | last post by:
Hi, I'm writing a commandline argument parser that can handle switches by using some techniques similar to the Qt slot mechanism. example: // inheritance method (what I'm trying at the...
3
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
10
by: makc.the.great | last post by:
now that I am looking at templates, there's the question. why same effect couldn't/shouldn't be achieved with inheritance? provided the difference of the two, when and where do I use templates...
11
by: Peter Oliphant | last post by:
Is there any plan to support templates with managed code in the (near) future? For instance, VS.NET 2005... : )
1
by: Michael | last post by:
Hello, I'm trying to implement sample I found on page templates, so I do not have to maintain "<html><head>..." on all my documents. But don't let that confuse you, this is an inheritance...
1
by: David Berman | last post by:
Hello, I'm having a problem using MapPath. The problem is that my production site is www.mydomain.com, but my development site is www.devdomain.com/myproject. In the root folder I have a base...
4
by: srinivasarao_moturu | last post by:
class ABC { public : virtual void operation1(); virtual void operation2(); virtual int GetValue(); virtual char GetValue(); virtual void SetValue(int); virtual void SetValue(char); }
4
by: RThaden | last post by:
Hi all, I looked in several books, articles, etc. but did not find a solution to my problem. Maybe somebody out there can help a desperate, not toooo experienced programmer: I want to...
5
by: Lars Hillebrand | last post by:
Hello, i discovered a weird behaviour if i use templates together with virtual inheritance and method over. I managed to reproduce my problem with a small example: // *********** <code...
6
by: pauldepstein | last post by:
Let double NR( double x, double(*)(const double&) f ) be the signature of a Newton-Raphson function NR. Here, f is a function which returns a double and accepts a const double&. The aim of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.