编译器说
error: expected constructor, destructor, or type conversion before ‘*’ token"
并指向.cpp中的这一行:
Node * Tree::buildTree(Node *myNode, int h) {
我猜它可能是关于typedef
但不确定的事情。问题是什么?
.h文件:
#ifndef TREE_H
#define TREE_H
class Tree {
public:
Tree();
Tree(Tree const & other);
~Tree();
Tree const & operator=(Tree const & other);
private:
class Node {
public:
Node() {
data = 0;
};
Node(Node const & other) {
_copy(other);
};
~Node() {};
Node const & operator=(Node const & other) {
if (this != &other)
_copy(other);
return *this;
};
Node *left;
Node *right;
int data;
private:
void _copy(Node const & other) {
data = other.data;
left = other.left;
right = other.right;
};
};
Node *root;
Node * buildTree(Node *myNode, int h);
};
.cpp文件:
...
Node * Tree::buildTree(Node *myNode, int h) {
if (h == 0)
return NULL;
myNode = new Node();
myNode->left = buildTree(myNode->left, h - 1);
myNode->right = buildTree(myNode->right, h - 1);
return myNode;
}
...
答案 0 :(得分:4)
Node
在Tree
内声明,因此您需要
Tree::Node * Tree::buildTree(Node *myNode, int h)