Declare another function of class C and return counter C

This is very similar to this question , however, the function I'm trying to make a friend returns a C enumerator. I cannot figure out the correct syntax:

#include <iostream>

extern "C" {
  enum X {ONE};
  int foo();
  X bar();
}

namespace a {
class A {
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
}

      

friend X ::bar();

does not work. What is the correct syntax for this.

Online demo

main.cpp:20:18: error: 'enum X' is not a class or a namespace
   friend X ::bar();
                  ^
main.cpp:20:18: error: ISO C++ forbids declaration of 'bar' with no type     [-fpermissive]
main.cpp: In function 'X bar()':
main.cpp:22:7: error: 'int a::A::a' is private
   int a;
       ^
main.cpp:35:18: error: within this context
   std::cout << a.a << std::endl;

      

+3


source to share


1 answer


Try adding parentheses to disambiguate parsing



friend X (::bar)();

      

+4


source







All Articles