c++ - Declare C function friend of class and return C enumerator -


this very simliar this question, however, function trying make friend returns c enumerator. cannot figure out correct syntax:

#include <iostream>  extern "c" {   enum x {one};   int foo();   x bar(); }  namespace { class { public:   a(int a): a(a) {} private:   friend int ::foo();   friend x ::bar();  // doesn't work   int a; }; }  extern "c" {  int foo() {   a::a a(1);   std::cout << a.a << std::endl;   return one; }  x bar() {   a::a a(2);   std::cout << a.a << std::endl;   return one; } }  int main() {   foo();  // outputs: 1   bar();  // doesn't compile } 

the friend x ::bar(); doesn't work. correct syntax that.

online demo

main.cpp:20:18: error: 'enum x' not class or namespace    friend x ::bar();                   ^ main.cpp:20:18: error: iso c++ forbids declaration of 'bar' no type     [-fpermissive] main.cpp: in function 'x bar()': main.cpp:22:7: error: 'int a::a::a' private    int a;        ^ main.cpp:35:18: error: within context    std::cout << a.a << std::endl; 

try adding parenthesis resolve parsing ambiguity

friend x (::bar)(); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -