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.
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
Post a Comment