相关疑难解决方法(0)

为什么我不能在类声明中放入"using"声明?

我理解当你using在头文件中放入一个声明时可以遇到的麻烦,所以我不想这样做.相反,我试图将using(或a namespace foo =)放在类声明中,以减少头文件中的重复输入.不幸的是我遇到编译错误.似乎它将是一个有用的功能.

#ifndef FOO_H
#define FOO_H

// This include defines types in namespace gee::whiz::abc::def,
// such as the class Hello.
#include "file_from_another_namespace.h"

// using namespace gee::whiz::abc::def; // BAD!

namespace x {
   namespace y {
      namespace z {

struct Foo {
    using namespace gee::whiz::abc::def; // Illegal.
    namespace other = gee::whiz::abc::def; // Illegal.

    // Foo(gee::whiz::abc::def::Hello &hello); // annoyingly long-winded

    Foo(other::Hello &hello); // better
    //...
};

} } } // end x::y::z namespace

#endif // FOO_H …
Run Code Online (Sandbox Code Playgroud)

c++ namespaces using compiler-errors header

18
推荐指数
1
解决办法
8223
查看次数

标签 统计

c++ ×1

compiler-errors ×1

header ×1

namespaces ×1

using ×1