typedef#39;d type not visible as return type of a member function(typedef 的类型作为成员函数的返回类型不可见)
问题描述
此程序无法编译(使用 gcc-4.5).错误消息说:
This program fails to compile(using gcc-4.5). The error message says:
错误:‘myType_t’没有命名类型
error: ‘myType_t’ does not name a type
1 class abc{
2 //typedef int myType_t;
3
4 public:
5 typedef int myType_t;
6
7 abc();
8 myType_t fun1();
9 };
10
11 myType_t abc::fun1()
12 {
13 return 0;
14 }
15
16 int main()
17 {
18 abc abc1;
19 return 0;
20 }
现在在 abc 类之外声明 typedef int myType_t; 使这个编译.我的困惑是,如果成员函数的返回类型在类中被 typedef,有什么问题.
Now declaring typedef int myType_t; outside the class abc makes this compile.
My confusion is, what is the problem if the return type of a member function is typedef'd inside the class.
推荐答案
来自 C++ 标准:
9.9 嵌套类型名称 [class.nested.type]
类型名称遵循与其他名称完全相同的范围规则.特别是,在类定义中定义的类型名称不能在没有限定的情况下在其类之外使用.
class X {
public :
typedef int I;
class Y { / . . . / };
I a;
};
I b; // error
Y c; // error
X::Y d; // OK
X::I e; // OK
所以你需要访问它:
abc::myType_t abc::fun1()
这篇关于typedef 的类型作为成员函数的返回类型不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:typedef 的类型作为成员函数的返回类型不可见
基础教程推荐
- 这个宏可以转换成函数吗? 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 在 C++ 中计算滚动/移动平均值 2021-01-01
