首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >声明类的C函数朋友并返回C枚举数

声明类的C函数朋友并返回C枚举数
EN

Stack Overflow用户
提问于 2015-05-15 10:35:47
回答 1查看 353关注 0票数 3

这对于这个问题来说非常简单,但是,我想要交友的函数返回一个C枚举数。我无法找出正确的语法:

代码语言:javascript
复制
#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();不起作用。正确的语法是什么。

在线演示

代码语言:javascript
复制
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;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-15 10:46:12

尝试添加括号以解决解析歧义。

代码语言:javascript
复制
friend X (::bar)();
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30257354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档