【问题标题】:Typdef from included file 'was not declared in this scope'包含文件中的 Typedef '未在此范围内声明'
【发布时间】:2018-08-03 14:43:22
【问题描述】:

我正在尝试使用已在包含的头文件中声明的 typedef,但出现以下错误:

error: ‘Status’ was not declared in this scope
   Status status;
   ^

typedef 在包含在文件中的标头中声明。

Server.hh(简体):

class myClass {
   public:
      typedef mynamespace::Status Status;
      ...
}

ClientServer.cc:

#include "Server.hh"
...
Status status;  // error thrown here

这种方法有问题吗?如何使我的 typedef 在多个文件中可用?

【问题讨论】:

  • 看起来你的 typedef 在一个类中。如果是这种情况,则名称的范围是类,而不是文件。
  • typedef 前面的public: 表示typedef 在类范围内,因此您可能需要className::Status 来获取typedef,但这纯粹是猜测。请提供minimal reproducible example
  • 是的,你是对的。将 typedef 移到 Server.hh 中的类定义之外可修复该错误。谢谢!
  • @user463035818 你应该发布你的答案,作为一个答案,所以它可以被接受
  • @NicholasPipitone 它不是一个答案,而只是一个猜测。另外,我不同意应该回答当前状态的问题。它应该通过添加缺失的信息 (mcve) 或关闭来修复

标签: c++


【解决方案1】:

typedefusingnamespace 都是写入语句的代码块本地的。您必须将 Server.hh 重新组织成

typedef mynamespace::Status Status;
class myClass {
   public:
      ...
}

Server.cc 访问它,因为typedef 将位于最外层代码块中。

【讨论】:

  • "local to the codeblock" 有点误导,可能将"codeblock" 替换为"scope"...(例如namespace foo { typedef int bar;} namespace foo { bar x;} 是两个代码块,但只有一个范围
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 2021-09-20
  • 2016-08-09
  • 2019-02-17
相关资源
最近更新 更多