Home > Software engineering >  getting error in understanding friend function in c
getting error in understanding friend function in c

Time:05-05

Why am I am getting an "expected identifier" error before .token?

Please provide a solution to understand friend function properly.

At first, I am getting a forward declaration error, but I resolved that one by myself.

#include<iostream>
using namespace std;

class a;

class b
{
public:
    void search(a and);
};

class a
{
    string name;
    friend void b::search(a and);
public:
};

void b::search(a and)
{
    cout << and.name;
}

int main()
{

}

CodePudding user response:

For compatibility with old keyboards and encoding schemes where some symbols weren't available, certain keywords can be used in place of symbols. Among them, and is a valid replacement for &&. So your and.name is actually getting parsed as &&.name, which is a syntax error.

The (unfortunate) solution to your problem is: Don't name variables and or any of the other words on that list.

  • Related