Re: no appropriate default constructor available??

Discussion in 'C++' started by Richard Herring, Oct 12, 2004.

  1. In message <>, Alan
    <> writes
    >
    >I would like to ask that why the compiler said there is no appropriate
    >default constructor available in my program. The following is the Node
    >class which will create a dynamic array, and each element in the array
    >is a pointer which will point to another Node type variable.


    Except it isn't.

    >Anybody can help? :)
    >
    >#ifndef node_h
    >#define node_h
    >#include <string>
    >#include "PrefixTree.h"
    >using namespace std;
    >
    >class Node{
    >private:
    > string data;
    > Node *array;


    This is a pointer to an array of Nodes, not an array of pointers. I
    suspect you mean

    Node * * array;
    ?

    > int counter;
    >public:
    > Node(string input):
    > data(input){
    > array = new Node[counter];


    1. counter has not yet been initialised.

    2. This tries to construct an array of Nodes, initialising each one with
    the default constructor, which is not defined. Hence your error message.

    3.[nitpick] Why not initialise array in the same way as data, instead of
    assigning in the body of the ctor ?

    Node (string input)
    : data(input),
    array(new Node* [something]),
    counter(something)
    {
    // assign pointers to elements of array?
    }


    4. You haven't defined a destructor to delete the array you allocated in
    the constructor.

    ~Node()
    {
    // destroy Nodes pointed to by elements of array?
    delete[] array;
    }

    5. Why use a counter and a dynamic array at all? This is the kind of
    thing std::vector was designed for.

    6. Don't forget that something, somewhere, will also need to construct
    and destruct the things to which the pointers point. You need to decide
    on an ownership model before proceeding. Some kind of smart pointer may
    be preferable to raw pointers.

    > }
    > friend class PrefixTree;
    >};
    >
    >#endif
    >
    >:confused:


    Very.

    --
    Richard Herring
     
    Richard Herring, Oct 12, 2004
    #1
    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. Duy Lam
    Replies:
    4
    Views:
    17,677
    John Harrison
    Jun 12, 2004
  2. Victor Bazarov
    Replies:
    0
    Views:
    523
    Victor Bazarov
    Oct 11, 2004
  3. Ook
    Replies:
    5
    Views:
    419
    Valentin Samko
    Oct 29, 2005
  4. NoNickName
    Replies:
    4
    Views:
    260
    Jim Langston
    Nov 1, 2005
  5. Replies:
    3
    Views:
    513
    n2xssvv g02gfr12930
    Feb 16, 2006
Loading...

Share This Page