c++ - 如何修复 “error: invalid use of non-static data member '树::root'” error in c++?

标签 c++ compiler-errors arguments

我在下面的代码:

#include <bits/stdc++.h>
#define DEFAULT_NODE_VALUE 0
using namespace std;


class node{
public:
    int val;
    node* right = 0;
    node* left = 0;
    node(int a):val(a){}

};

class tree{
public:
    node* root = new node(DEFAULT_NODE_VALUE);
    tree(int inp_val){
        root->val = inp_val; 
    }

    void inorder_traverse(node* temp = root){
        
    }
    
};
我想在temp函数中为inorder_traverse参数设置默认参数,但是
当我编译以上代码时,编译器显示如下:
22:37: error: invalid use of non-static data member 'tree::root'
17:8: note: declared here
请帮助我解决此问题。提前致谢...

最佳答案

参数默认值不在方法范围内评估。不要使用默认参数,而应使用重载。

void inorder_traverse() {
    inorder_traverse(root);
}
void inorder_traverse(node *temp) {
    // code
}

关于c++ - 如何修复 “error: invalid use of non-static data member '树::root'” error in c++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65462696/

相关文章:

objective-c - iOS performSelectorOnMainThread 有多个参数

c++ - 在通信缓冲区中混合 int 和 float

c++ - 强制派生类实现私有(private)方法

c - 为什么编译器说变量 "z"被重新声明为不同的变量类型?

java - 如何找到 "already defined in an unnamed package"的 Java 类?

c - 用可变数量的参数包装宏

HASKELL Lambda 表达式 ‘\xs -> ...’ 有一个参数,但它的类型 ‘[t]’ 没有

c++ - 等价于 C 中的 std::aligned_storage<>?

c++ - 如何创建我的 C++ 代码的 dll。

c - 如何将全局变量放置在相邻位置? (无需在链接描述文件中设置)