今天开发的时候,发现用模板的时候,再加上iterator,会报错,如下:

std::map<T, S>::iterator find_iter = mp.find(key);

编译会报错:

error: expected `;' before "find_iter"

 

查了这篇文章:

https://stackoverflow.com/questions/11275444/c-template-typename-iterator

 

发现要在前面加typename,如下:

typename std::map<T, S>::iterator find_iter = mp.find(key);

 

另外,如果传进来的参数是const的,那么iterator也需要是const_iterator才行。

 

相关文章:

  • 2021-09-19
  • 2022-12-23
  • 2021-12-20
  • 2022-12-23
  • 2021-10-20
  • 2021-07-09
  • 2021-04-29
  • 2022-01-19
猜你喜欢
  • 2022-12-23
  • 2022-01-04
  • 2022-12-23
  • 2021-10-28
  • 2022-12-23
  • 2021-08-01
  • 2021-05-26
相关资源
相似解决方案