Why can#39;t I put a quot;usingquot; declaration inside a class declaration?(为什么我不能放一个“使用?类声明中的声明?)
问题描述
我理解将 using 声明放入头文件时可能会遇到的麻烦,所以我不想这样做.相反,我尝试将 using(或 命名空间 foo =)放在类声明中,以减少头文件中的重复输入.不幸的是,我收到编译器错误.看起来这将是一个有用的功能.
I understand the troubles you can get into when you put a using declaration inside a header file, so I don't want to do that. Instead I tried to put the using (or a namespace foo =) within the class declaration, to cut down on repetitive typing within the header file. Unfortunately I get compiler errors. Seems like it would be a useful feature.
#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
在实际代码中,命名空间名称要长得多且烦人,而且我无法更改.
In the real code, the namespace names are much longer and annoying and it's not something I can change.
谁能解释为什么这不合法,或者(更好)是否有解决方法?
Can anyone explain why this is not legal, or (better) if there's a workaround?
推荐答案
你能做 typedef gee::whiz::abc::def::Hello Hello 吗?
这篇关于为什么我不能放一个“使用"?类声明中的声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我不能放一个“使用"?类声明中的声明?
基础教程推荐
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
