声明 class 和 return C 枚举器的 C 函数友元
Declare C function friend of class and return C enumerator
这与 非常 相似,但是,我正在尝试让朋友 returns 成为 C 枚举器的函数。我想不出正确的语法:
#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();
不起作用。正确的语法是什么。
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;
尝试添加括号来解决解析歧义
friend X (::bar)();
这与
#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();
不起作用。正确的语法是什么。
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;
尝试添加括号来解决解析歧义
friend X (::bar)();