Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 带模板的typedef 模板结构A{ typedef int; A::Int b;//第1行(失败) Int c;//第2行(编译) }; int main(){ A x; x、 c=13; }_C++_Templates_Typedef - Fatal编程技术网

C++ 带模板的typedef 模板结构A{ typedef int; A::Int b;//第1行(失败) Int c;//第2行(编译) }; int main(){ A x; x、 c=13; }

C++ 带模板的typedef 模板结构A{ typedef int; A::Int b;//第1行(失败) Int c;//第2行(编译) }; int main(){ A x; x、 c=13; },c++,templates,typedef,C++,Templates,Typedef,错误 template<class T> struct A { typedef int Int; A::Int b; // Line 1 (fails) Int c; // Line 2 (compiles) }; int main(){ A<int> x; x.c = 13; } 错误:ISO C++禁止声明“INT”,没有类型 错误:成员“Int”上的额外限定“A::” 错误:应为“;”在“b”之前 第1行失败,但第2

错误

template<class T> struct A {
    typedef int Int;
    A::Int b; // Line 1 (fails)
    Int c; // Line 2 (compiles)
};

int main(){    
   A<int> x;
   x.c = 13;
}
<代码>错误:ISO C++禁止声明“INT”,没有类型 错误:成员“Int”上的额外限定“A::” 错误:应为“;”在“b”之前
第1行失败,但第2行编译。为什么?

您需要一个
typename

error: ISO C++ forbids declaration of ‘Int’ with no type
error: extra qualification ‘A<T>::’ on member ‘Int’
error: expected ‘;’ before ‘b’
typename
关键字是必需的,因为引用成员时使用了限定名
a::Int

Int c
可以,因为在这种情况下没有使用限定名称

14.6/6

在类模板的定义或类模板成员的定义中,当引用以前声明的成员的非限定名称时,不需要关键字typename 声明类型的类模板的。输入时,应始终指定关键字typename 使用限定名引用成员,即使限定符只是类模板名


你的代码在g++4.4.5上用-Wall编译,你得到了什么错误消息?在东西上粘贴typename可以解决所有问题。你真的需要在限定的依赖类型(read)之前使用typename,
A::Int
依赖类型是什么?它不因t而变化。@Prasoon:它是一种依赖类型
A::Int
A::Int
。在类中,类型参数是隐式的。@Chris这是一个正确的观察结果。这在C++0x中是固定的,它使名称
A::Int
不再依赖,不再需要
typename
。@vij(这些段落与FDI相比有很大的变化,但#224抓住了基本要点)。
typename A::Int b;