C++ 错误:“int 之前的预期主表达式”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9534728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:56:38  来源:igfitidea点击:

Error: "expected primary-expression before int"

c++

提问by visanio_learner

I am getting an error: expected primary-expression before intwhen I try to return a 2 values in bool function, I think its a member function error.

我收到一个错误:int当我尝试在 bool 函数中返回 2 个值之前预期的主表达式,我认为它是一个成员函数错误。

bool binaryTreeTraversal::LeafNode(int node)
{
        return (binaryTreeTraversal::LeftPtr(int node) == NULL &&   
        binaryTreeTraversal::RightPtr(int node) == NULL);

}

class binaryTreeTraversal

{
public:
    int TreeNodes[2^5];
    int size;
    binaryTreeTraversal(void);
    bool LeafNode(int node);
    int RootNode(int node);
    int LeftPtr(int node);
    int RightPtr(int node);
    int length();
    int preOrderTraversal(int);
    int inOrderTraversal(int);
    int postOrderTraversal(int);
};

bool binaryTreeTraversal::LeafNode(int node)
{
    return (binaryTreeTraversal::LeftPtr(node) == NULL &&
            binaryTreeTraversal::RightPtr(node) == NULL);
}

回答by Raymond Chen

Making this question useful to others: "expected primary-expression" means "I thought you were going to put an expression here."

让这个问题对其他人有用:“预期的主要表达”的意思是“我以为你会在这里放一个表达。”

An expression is an object, a function call, or operators applied to objects and function calls . For example, x + f(y)is an expression involving variables xand y, the function fand the operator +.

表达式是一个对象、一个函数调用或应用于对象和函数调用的运算符。例如,x + f(y)是一个涉及变量xy、函数f和运算符的表达式+

There are many types of expressions, but the distinction isn't important for the purpose of this error message.

有多种类型的表达式,但对于此错误消息的目的而言,区别并不重要。

After the open-parenthesis denoting a function call, you are expected to enter an expression, representing the value to pass as a parameter to the function call. But instead, the compiler saw the word int, which is not a variable or a function or an operator.

在表示函数调用的左括号之后,您需要输入一个表达式,表示作为参数传递给函数调用的值。但相反,编译器看到了单词int,它不是变量、函数或运算符。

回答by hmjd

Change:

改变:

return (binaryTreeTraversal::LeftPtr(int node) == NULL &&
        binaryTreeTraversal::RightPtr(int node) == NULL);

to:

到:

return (binaryTreeTraversal::LeftPtr(node) == NULL &&
        binaryTreeTraversal::RightPtr(node) == NULL);

EDIT:

编辑:

The return type from LeftPtr()and RightPtr()is int:

从返回类型LeftPtr()RightPtr()int

class binaryTreeTraversal
{
public:
    ...
    bool LeafNode(int node)
    {
        return (binaryTreeTraversal::LeftPtr(node) == 0 &&
                binaryTreeTraversal::RightPtr(node) == 0);
    }
};

or:

或者:

class binaryTreeTraversal
{
public:
    ...
    bool LeafNode(int node)
    {
        return (!binaryTreeTraversal::LeftPtr(node) &&
                !binaryTreeTraversal::RightPtr(node));
    }
};

or as LeftPtr()and RightPtr()are not virtual:

或作为LeftPtr()RightPtr()不是virtual

class binaryTreeTraversal
{
public:
    ...
    bool LeafNode(int node)
    {
        return (!LeftPtr(node) && !RightPtr(node));
    }
};

回答by Stefan Birladeanu

return (binaryTreeTraversal::LeftPtr(int node) == NULL &&
binaryTreeTraversal::RightPtr(int node) == NULL);

return (binaryTreeTraversal::LeftPtr( int node) == NULL &&
binaryTreeTraversal::RightPtr( int node) == NULL);

//remove int from bolded part

//从粗体部分删除int

回答by rob05c

    return (binaryTreeTraversal::LeftPtr(int node) == NULL && binaryTreeTraversal::RightPtr(int node) == NULL);

should be

应该

return (binaryTreeTraversal::LeftPtr(node) == NULL && binaryTreeTraversal::RightPtr(node) == NULL);

回答by spmno

bool binaryTreeTraversal::LeafNode(int node)
{
    return ((LeftPtr(node) == NULL) && (RightPtr(node) == NULL));
}