小编Jas*_*rya的帖子

错误:非常量静态数据成员必须在行外初始化

class Solution {
    public:

     static int m=INT_MIN; // it shows error: non-const static data member must 
                               be initialized out of line.(why?)
                                  using "int m=INT_MIN" is fine. 
      int func(TreeNode*root){
        if(root==NULL){
            return 0;
        }

        int l=max(func(root->left),0);
        int r=max(func(root->right),0);

        m=max(l+r+root->val,m);

        return max(l,r)+root->val;

    }


    int maxPathSum(TreeNode* root) {

        if(root==NULL)
        {
         return 0;
        }
        m=INT_MIN;
        int x=func(root);
        return m;

    }
};
Run Code Online (Sandbox Code Playgroud)

我需要更新 variable 的值m。因此我使用static int数据类型。但是下面的错误来了。使用int而不是static int工作正常。但是为什么会static int报错呢?

编译错误

c++ static class

7
推荐指数
2
解决办法
3374
查看次数

标签 统计

c++ ×1

class ×1

static ×1