STL iterator with custom template

倖福魔咒の 提交于 2020-01-02 05:39:05

问题


i have the following template method,

template <class T>
void Class::setData( vector<T> data )
{    
    vector<T>::iterator it;
}

and i'm getting the following compilation error ( XCode/gcc )

error: expected `;' before 'it'

i found someone else with a similar problem here (read down to see it's the same even though it starts out with a different issue) but they seem to have resolved by updating Visual Studio. This makes me guess that it is a compiler issue and that it should compile, is that correct? Iteration via indexing from 0 to size works, however it is not the way i would prefer to implement this function. Is there another way around this? Thanks


回答1:


Classic case of when to use the typename keyword. Hoping that you have #include-ed vector and iterator and have a using namespace std; somewhere in scope. Use:

typename vector<T>::iterator it;

Look up dependent names. Start here.




回答2:


I think you are missing a typename:

#include <vector>
using namespace std;

class Class{
public:
    template <class T>
    void setData( vector<T> data ) {
        typename vector<T>::iterator it;
    }
};



回答3:


Try:

template <class T>
void Class::setData( std::vector<T> data )
{    
    std::vector<T>::iterator it;
}

Just is case it's a missing using statement?



来源:https://stackoverflow.com/questions/594521/stl-iterator-with-custom-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!