allocate memory for a list array with templates

Discussion in 'C++' started by OlgaM, Oct 10, 2003.

  1. OlgaM

    OlgaM Guest

    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
    OlgaM, Oct 10, 2003
    #1
    1. Advertising

  2. OlgaM

    Mike Wahler Guest

    "OlgaM" <> wrote in message
    news:...
    > 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
    Mike Wahler, Oct 10, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    3
    Views:
    468
  2. bijax
    Replies:
    7
    Views:
    357
    Daniel Rudy
    Sep 20, 2005
  3. valerio
    Replies:
    3
    Views:
    370
  4. recover
    Replies:
    2
    Views:
    800
    recover
    Jul 25, 2006
  5. Pawel_Iks
    Replies:
    4
    Views:
    547
    Pawel_Iks
    Oct 29, 2006
Loading...

Share This Page