C++编译报错:need 'typename' before 'std::map<T, S>::iterator' because 'std::map<T, S>' is a dependent scope

使用C++的模板函数做展示输出

template <typename T>
    inline void displayVector(vector<T>& vec){
    	for(T v : vec)cout << v;
    	cout << endl;
	}
	
	template <typename T, typename S>
	inline void displayMap(map<T, S>& mp){
		for(typename map<T, S>::iterator iter = mp.begin(); iter!=mp.end(); iter++)cout << iter->first << " " << iter->second << endl;
	}
	
	template <typename T, typename S>
	inline void displayMapVector(map<T, vector<S>>& mp){
		for(typename map<T, vector<S>>::iterator iter = mp.begin(); iter != mp.end(); iter++)cout << iter->first << " " << displayVector(iter->second);
	}

报错后在map::iterator前面加个typename声明模板类型是类型名即可。
参考链接:
Error: need ‘typename’ before iterator [duplicate]
编译错误need 'typename' before *** because *** is a dependent scope 浅析

posted @ 2020-10-31 23:51  园糯  阅读(1525)  评论(0编辑  收藏  举报