Issue
I have the following program that compiles successfully on my machine using GCC 7.5.0 but when i try out the program here the program doesn't work.
class Foo
{
friend void ::error() { }
};
int main()
{
return 0;
}
The error here says:
Compilation failed due to following error(s).
3 | friend void ::error() { }
| ^
My question what is the problem here and how can i solve it.
Solution
The problem is that a qualified friend declaration cannot be a definition. In your example, since the name error
is qualified(because it has ::
) so the corresponding friend declaration cannot be a definition.
This seems to be a bug in GCC 7.5.0. For example, for gcc 7.5.0 the program works but for gcc 8.4.0 and higher it doesn't work.
One way to solve thiswould be to remove the body { }
of the error
function and make it a declaration by adding semicolon ;
as shown below:
//forward declare function error()
void error();
class Foo
{
friend void ::error();//removed the body { } . This is now a declaration and not a definition
};
int main()
{
return 0;
}
//definition of function error
void error()
{
}
Note you can also put the definition of function error()
before the definition of class Foo
as shown here.
Another way to solve this would be to remove the ::
and make error
an unqualified name as shown here
Answered By - Anoop Rana Answer Checked By - Katrina (WPSolving Volunteer)