下面的代码在C++中遇到了value is not usable in a constant expression错误
int sumNumbers(TreeNode* root) {
stack<pair<TreeNode*, int>> st;
st.push(make_pair(root, root->val));
int sum = 0;
while(!st.empty()){
pair<TreeNode*, int> temp = st.top();
st.pop();
TreeNode* node = temp.first;
int value = temp.second;
if(node->left==NULL && node->right==NULL){
sum += value;
}
if(node->left){
st.push(pair< node->left, value*10 + node->left->val >);
}
if(node->right){
st.push(pair< node->right, value*10 + node->right->val >);
}
}
return sum;
}
错误在行中:
if(node->left){
st.push(pair< node->left, value*10 + node->left->val >);
}
错误是:
Line 29: Char 37: error: the value of 'node' is not usable in a constant expression st.push(pair< node->left, value*10 + node->left->val >);
我无法弄清楚为什么会在此处遇到此错误?
最佳答案
您想使用make_pair
来创建一个对象,自动设置模板类型:
st.push(make_pair(node->left, value*10 + node->left->val));
右边也一样。
关于c++ - 值在常量表达式 C++ 中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54488756/