allocate memory for a list array with templates

O

OlgaM

Hello,

i'm trying to initialize an array.

The class List contains this in its private data members:

ListNode<DATATYPE> *dataItems;

In the constructor, i'm trying to allocate space for this array.

dataItems=new DATATYPE[maxNumber];

I'm getting the compiler error
'=' : cannot convert from 'int *' to 'class ListNode<int> *'

what am i doing wrong? Help please.

Olga Mednik
(e-mail address removed)
 
M

Mike Wahler

OlgaM said:
Hello,

i'm trying to initialize an array.

The class List contains this in its private data members:

ListNode<DATATYPE> *dataItems;

In the constructor, i'm trying to allocate space for this array.

dataItems=new DATATYPE[maxNumber];

dataItems = new ListNode<DATATYPE>[maxNumber];

... and if you haven't done so already, don't forget to
create your copy constructor, assignment operator, and
destructor ("rule of three"), to do the 'delete[]'s
and/or reallocations. [1]
I'm getting the compiler error
'=' : cannot convert from 'int *' to 'class ListNode<int> *'

what am i doing wrong? Help please.

[1] You can preempt many headaches and bugs if you
use a container (e.g. a std::vector), instad of an array.
All the memory management would be done for you.

std::vector<ListNode<DATATYPE> > dataItems;

And unless you're making your own linked list
for learning purposes, you might consider using
the standard library's list class (which also
manages its own memory):

std::list<DATATYPE> dataItems;

HTH,
-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top