头文件中声明的typedef在源文件中不可用
typedef declared in header file not available in source file
我在头文件中定义了一个类myclass
,在private:
部分中定义了typedef
。
typedef int inttest;
我的源文件包括这个头文件,但当试图像一样使用源文件中的typedef时
inttest myclass::foo() { }
我得到错误:
error: 'inttest' does not name a type
为什么会这样?我是否还需要在源文件中声明typedef
?
首先,typedef是在类的作用域中定义的。因此,如果将typedef用作返回类型的非限定名称,编译器将找不到它的定义。你可以写例如
myclass::inttest myclass::foo() { }
但是,编译器将再次发出错误,因为typedef被定义为private。
编辑:对不起。我展示的函数的定义将被编译。
然而,在调用函数的代码中,您需要编写
myclass a;
int i = a.foo();
或
myclass a;
auto i = a.foo();
你可以不写
myclass a;
myclass::inttest i = a.foo();
相关文章:
- C++:TypeDef使用元组
- 为测试目标创建具有不同源文件夹的文件
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 将图像添加到资源文件夹UWP C++
- 为什么在使用typedef时类推导指南会失败
- 视觉工作室项目.提取源文件夹名称
- 生成一个生成文件,该生成文件使用Automake在一个步骤中编译和链接所有源文件
- 尝试根据类中 typedef 的存在来专门化模板函数
- 如何重新定义MPI_FLOAT,MPI_DOUBLE以 typedef 的方式
- typedef 枚举和枚举类有什么区别?
- make 命令如何避免重新编译未更改的源文件?
- C++两个源文件之间共享的枚举的静态实例
- 在类模板中使用 typedef 时出错
- 如何使用Qt 3D库加载和显示搅拌机.obj源文件场景
- 模板类中的 typedef 语句
- 在 typedef 内部使用 const 关键字和在 typedef 外部使用 const 关键字之间有区别吗?
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 生成文件:动态源文件名和对象目录
- 在 C++ 中将 typedef 函数从标头实现到源文件中
- 头文件中声明的typedef在源文件中不可用