关于c ++:包含文件中的Typedef’未在此范围内声明’


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

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

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

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

Server.hh(简体):

1
2
3
4
5
class myClass {
   public:
      typedef mynamespace::Status Status;
      ...
}

ClientServer.cc:

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

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


typedefusingnamespace均位于编写该语句的代码块的本地。您必须将Server.hh重组为

1
2
3
4
5
typedef mynamespace::Status Status;
class myClass {
   public:
      ...
}

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