Re: no appropriate default constructor available??

R

Richard Herring

Alan said:
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.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top