在'*'标记之前的预期构造函数,析构函数或类型转换

时间:2013-03-10 21:28:08

标签: c++ inner-classes

编译器说

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;
}

...

1 个答案:

答案 0 :(得分:4)

NodeTree内声明,因此您需要

Tree::Node * Tree::buildTree(Node *myNode, int h)