c++模版函数中应用typename
2011年06月21日
1>------ Build started: Project: test_is_Bsearch_postOrderTravel, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>e:\c++\test_is_bsearch_postordertravel\main.cpp(6 ) : warning C4346: 'std::vector::iterator' : dependent name is not a type
1> prefix with 'typename' to indicate a type
1>e:\c++\test_is_bsearch_postordertravel\main.cpp(6 ) : error C2061: syntax error : identifier 'iterator'
1>Build log was saved at "file://e:\C++\test_is_Bsearch_postOrderTravel\Deb ug\BuildLog.htm"
1>test_is_Bsearch_postOrderTravel - 1 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
可以正常的编译通过。具体原因可能是编译器不能确定vector::iterator是个类型还是个变量。可能跟编译器的实现有关系。
2011年06月21日
1>------ Build started: Project: test_is_Bsearch_postOrderTravel, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>e:\c++\test_is_bsearch_postordertravel\main.cpp(6 ) : warning C4346: 'std::vector::iterator' : dependent name is not a type
1> prefix with 'typename' to indicate a type
1>e:\c++\test_is_bsearch_postordertravel\main.cpp(6 ) : error C2061: syntax error : identifier 'iterator'
1>Build log was saved at "file://e:\C++\test_is_Bsearch_postOrderTravel\Deb ug\BuildLog.htm"
1>test_is_Bsearch_postOrderTravel - 1 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
可以正常的编译通过。具体原因可能是编译器不能确定vector::iterator是个类型还是个变量。可能跟编译器的实现有关系。
本文探讨了在C++模板函数中使用typename关键字的重要性。当编译器无法判断vector::iterator是否为类型时,需要使用typename来明确指示。此问题与编译器实现有关。

被折叠的 条评论
为什么被折叠?



