Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: "expected primary-expression before int"

Tags:

c++

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

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);
}
like image 601
visanio_learner Avatar asked Aug 24 '26 19:08

visanio_learner


2 Answers

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 x and y, the function f and the operator +.

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.

like image 158
Raymond Chen Avatar answered Aug 26 '26 09:08

Raymond Chen


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:

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:

class binaryTreeTraversal
{
public:
    ...
    bool LeafNode(int node)
    {
        return (!LeftPtr(node) && !RightPtr(node));
    }
};
like image 39
hmjd Avatar answered Aug 26 '26 10:08

hmjd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!