This might be a stupid question but I can't find a lot of information on the web about creating your own default constructors in C++. It seems to just be a constructor with no parameters. However, I tried to create my default constructor like this:
Tree::Tree() {root = NULL;}
I also tried just:
Tree::Tree() {}
When I try either of these I am getting the error:
No instance of overloaded function "Tree::Tree" matches the specified type.
I can't seem to figure out what this means.
I am creating this constructor in my .cpp
file. Should I be doing something in my header (.h
) file as well?
Member functions (and that includes constructors and destructors) have to be declared in the class definition:
class Tree {
public:
Tree(); // default constructor
private:
Node *root;
};
Then you can define it in your .cpp file:
Tree::Tree() : root(nullptr) {
}
I threw in the nullptr
for C++11. If you don't have C++11, use root(0)
.