I have a class myclass
defined in a header file with a typedef
in the private:
section.
我在头文件中定义了一个类myclass,在private:section中有一个typedef。
typedef int inttest;
My source file includes this header file, yet when attempting to use the typedef in the source file like so
我的源文件包含此头文件,但尝试在源文件中使用typedef时是这样的
inttest myclass::foo() { }
I get the error:
我收到错误:
error: 'inttest' does not name a type
Why is this? Do I need to also declare the typedef
in the source file?
为什么是这样?我是否还需要在源文件中声明typedef?
1 个解决方案
#1
3
First of all the typedef is defined in the scope of the class. So the compiler can not find the definition of the typedef if it is used as unqualified name as a return type. You could write for example
首先,typedef在类的范围内定义。因此,如果将编译器用作非限定名称作为返回类型,则编译器无法找到typedef的定义。你可以写一些例子
myclass::inttest myclass::foo() { }
However the compiler again will issue an error because the typedef is defined as private.
但是,编译器会再次发出错误,因为typedef被定义为private。
EDIT: I am sorry. The definition of the function I showed will be compiled.
编辑:对不起。我将展示我所展示的功能的定义。
However in the code that calls the function you will need to write either
但是在调用该函数的代码中,您还需要编写
myclass a;
int i = a.foo();
or
要么
myclass a;
auto i = a.foo();
You may not write
你可能不写
myclass a;
myclass::inttest i = a.foo();
#1
3
First of all the typedef is defined in the scope of the class. So the compiler can not find the definition of the typedef if it is used as unqualified name as a return type. You could write for example
首先,typedef在类的范围内定义。因此,如果将编译器用作非限定名称作为返回类型,则编译器无法找到typedef的定义。你可以写一些例子
myclass::inttest myclass::foo() { }
However the compiler again will issue an error because the typedef is defined as private.
但是,编译器会再次发出错误,因为typedef被定义为private。
EDIT: I am sorry. The definition of the function I showed will be compiled.
编辑:对不起。我将展示我所展示的功能的定义。
However in the code that calls the function you will need to write either
但是在调用该函数的代码中,您还需要编写
myclass a;
int i = a.foo();
or
要么
myclass a;
auto i = a.foo();
You may not write
你可能不写
myclass a;
myclass::inttest i = a.foo();