包含文件中的 Typdef "未在此范围内声明"

Typdef from included file 'was not declared in this scope'

本文关键字:范围内 声明 包含 Typdef 文件      更新时间:2023-10-16

我试图使用一个在包含的头文件中声明的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在多个文件中可用?

typedefusingnamespace都是编写语句的代码块的本地。您必须将Server.hh重组为

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

以便Server.cc访问它,因为typedef将在最外面的代码块中。