"Node" not declared in scope 错误,即使 "Node" 在 Tree class 中声明
"Node" not declared in scope error even though "Node" is declared inside Tree class
我无法弄清楚为什么会出现编译错误
5 class Tree {
6 public:
7 Tree() : root(nullptr) { std::cout << "tree created" << std::endl; }
8 ~Tree() { std::cout << "tree destroyed" << std::endl; }
9 bool add(int val);
10 void inOrderTraversal() { inOrderTraversal(root); }
11
12 private:
13 struct Node {
14 int data;
15 std::shared_ptr<Node> left;
16 std::shared_ptr<Node> right;
17 Node(int val) : data(val), left(nullptr), right(nullptr) { std::cout << "tree node created: " << data << std::endl; }
18 ~Node() { std::cout << "tree node destroyed: " << data << std::endl; }
19 };
20
21 void inOrderTraversal(std::shared_ptr<Node> node);
22 std::shared_ptr<Node> insert(int val, std::shared_ptr<Node> subTree);
23 std::shared_ptr<Node> root;
24 };
25
26 std::shared_ptr<Node> Tree::insert(int val, std::shared_ptr<Node> subTree) {
27 if (subTree == nullptr) {
28 std::shared_ptr<Node> node = std::make_shared<Node>(val);
29 return node;
30 }
31
32 if (val <= subTree->data) {
33 subTree->left = insert(val, subTree->left);
34 } else {
35 subTree->right = insert(val, subTree->right);
36 }
37 return subTree;
38 }
但是我得到以下编译错误:
g++ -std=c++11 boot_camp.cpp
boot_camp.cpp:26:17: error: ‘Node’ was not declared in this scope
std::shared_ptr<Node> Tree::insert(int val, std::shared_ptr<Node> subTree) {
^
boot_camp.cpp:26:21: error: template argument 1 is invalid
std::shared_ptr<Node> Tree::insert(int val, std::shared_ptr<Node> subTree) {
^
boot_camp.cpp:26:23: error: prototype for ‘int Tree::insert(int, std::shared_ptr<Tree::Node>)’ does not match any in class ‘Tree’
std::shared_ptr<Node> Tree::insert(int val, std::shared_ptr<Node> subTree) {
我不明白为什么它会抱怨“节点”未在范围内声明。
我确实在树 class.
中声明了“节点”
而且我没有在其他函数中看到错误,例如:
55 void Tree::inOrderTraversal(std::shared_ptr<Node> node) {
56 if (node) {
57 inOrderTraversal(node->left);
58 std::cout << node->data << " ";
59 inOrderTraversal(node->right);
60 }
61 }
Node
是 Tree
的内部 class。将 class 定义之外的引用更改为 Tree::Node
,以便 g++ 在正确的命名空间中查找您的定义。
像这样:
std::shared_ptr<Tree::Node> Tree::insert(int val, std::shared_ptr<Node> subTree) {
...
}
您还需要在插入函数中修复 return true
,因为您应该 return 改为 std::shared_ptr<Tree::Node>
。
此外,Suhas 在评论中提出了一个很好的观点,我没有注意到您只需要 return 值的 Tree::
范围(例如 Tree::inOrderTraversal
)。这似乎是因为在函数 inOrderTraversal
内,编译器将根据 C++ 11 Standard Section 3.4.1 非限定名称查找规则在现有 class 中查找 Node
的定义。它不会对函数之外的 return 值应用此规则。
我无法弄清楚为什么会出现编译错误
5 class Tree {
6 public:
7 Tree() : root(nullptr) { std::cout << "tree created" << std::endl; }
8 ~Tree() { std::cout << "tree destroyed" << std::endl; }
9 bool add(int val);
10 void inOrderTraversal() { inOrderTraversal(root); }
11
12 private:
13 struct Node {
14 int data;
15 std::shared_ptr<Node> left;
16 std::shared_ptr<Node> right;
17 Node(int val) : data(val), left(nullptr), right(nullptr) { std::cout << "tree node created: " << data << std::endl; }
18 ~Node() { std::cout << "tree node destroyed: " << data << std::endl; }
19 };
20
21 void inOrderTraversal(std::shared_ptr<Node> node);
22 std::shared_ptr<Node> insert(int val, std::shared_ptr<Node> subTree);
23 std::shared_ptr<Node> root;
24 };
25
26 std::shared_ptr<Node> Tree::insert(int val, std::shared_ptr<Node> subTree) {
27 if (subTree == nullptr) {
28 std::shared_ptr<Node> node = std::make_shared<Node>(val);
29 return node;
30 }
31
32 if (val <= subTree->data) {
33 subTree->left = insert(val, subTree->left);
34 } else {
35 subTree->right = insert(val, subTree->right);
36 }
37 return subTree;
38 }
但是我得到以下编译错误:
g++ -std=c++11 boot_camp.cpp
boot_camp.cpp:26:17: error: ‘Node’ was not declared in this scope
std::shared_ptr<Node> Tree::insert(int val, std::shared_ptr<Node> subTree) {
^
boot_camp.cpp:26:21: error: template argument 1 is invalid
std::shared_ptr<Node> Tree::insert(int val, std::shared_ptr<Node> subTree) {
^
boot_camp.cpp:26:23: error: prototype for ‘int Tree::insert(int, std::shared_ptr<Tree::Node>)’ does not match any in class ‘Tree’
std::shared_ptr<Node> Tree::insert(int val, std::shared_ptr<Node> subTree) {
我不明白为什么它会抱怨“节点”未在范围内声明。 我确实在树 class.
中声明了“节点”而且我没有在其他函数中看到错误,例如:
55 void Tree::inOrderTraversal(std::shared_ptr<Node> node) {
56 if (node) {
57 inOrderTraversal(node->left);
58 std::cout << node->data << " ";
59 inOrderTraversal(node->right);
60 }
61 }
Node
是 Tree
的内部 class。将 class 定义之外的引用更改为 Tree::Node
,以便 g++ 在正确的命名空间中查找您的定义。
像这样:
std::shared_ptr<Tree::Node> Tree::insert(int val, std::shared_ptr<Node> subTree) {
...
}
您还需要在插入函数中修复 return true
,因为您应该 return 改为 std::shared_ptr<Tree::Node>
。
此外,Suhas 在评论中提出了一个很好的观点,我没有注意到您只需要 return 值的 Tree::
范围(例如 Tree::inOrderTraversal
)。这似乎是因为在函数 inOrderTraversal
内,编译器将根据 C++ 11 Standard Section 3.4.1 非限定名称查找规则在现有 class 中查找 Node
的定义。它不会对函数之外的 return 值应用此规则。