我有一个类myclass
的定义,定义在头文件中,在private:
部分有一个typedef
。
typedef int inttest;
我的源文件包括这个头文件,然而当我试图在源文件中使用typedef时,就像这样
inttest myclass::foo() { }
I get the error:
error: 'inttest' does not name a type
为什么会这样?我是否还需要在源文件中声明
typedef
?我有一个类myclass
的定义,定义在头文件中,在private:
部分有一个typedef
。
typedef int inttest;
inttest myclass::foo() { }
I get the error:
error: 'inttest' does not name a type
typedef
?首先,typedef 在类的作用域中定义。因此,如果将其用作未限定名称作为返回类型,编译器将无法找到 typedef 的定义。您可以编写以下内容:
myclass::inttest myclass::foo() { }
myclass a;
int i = a.foo();
或者
myclass a;
auto i = a.foo();
您可能不会编写
myclass a;
myclass::inttest i = a.foo();