我理解当你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)