Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 views
in Technique[技术] by (71.8m points)

c++ - dependent scope; need typename in front;

I want to create a template as follows. I want to delete a list of items from vector vec1. And the indexes of the items I want to delete are stored in index_list.

#include <vector>

using namespace std;

template <typename a_type>
bool vector_remove(vector< a_type > & vec1, vector< int > index_list)
{
    //index_list is sorted in order from small to large.

    if(index_list.size() > vec1.size())
    {
        cout << "ERROR in 'vector_remove()': index_list is longer than vec1."<<endl;
        return false;
    }
    if(index_list.size() == vec1.size())
    {
        vec1.clear();
        return true;
    }
    vector< int >::iterator ind_pt = index_list.begin();
    vector< a_type >::iterator vec1_pre = vec1.begin();
    vector< a_type >::iterator vec1_pos = vec1.begin();
    int vec1_ind = 0;
    while(ind_pt != index_list.end() && vec1_pos != vec1.end())
    {
        if(*ind_pt == vec1_ind)
        {
            ind_pt ++;
            vec1_pos ++;
            vec1_ind ++;
        }
        else if( *ind_pt > vec1_ind )
        {
            *(vec1_pre) = *(vec1_pos);
            vec1_pos ++;
            vec1_pre ++;
            vec1_ind ++;
        }
        else
        {
            cout << "ERROR in 'vector_remove'." <<endl;
            return false;
        }
    }
    while(vec1_pos != vec1.end())
    {
        *(vec1_pre) = *(vec1_pos);
        vec1_pos ++;
        vec1_pre ++;
    }
    // the above codes are to put all the rejected elements to the end of the vec1.

    // pop back all the rejected elements.
    while(vec1_pre != vec1.end() )
    {
        vec1.pop_back();
    }

    return true;
}

But it returns a lot of errors:

In file included from demo.cpp:3:0:
my_vector.h: In function ‘bool vector_remove(std::vector<a_type>&, std::vector<int>)’:
my_vector.h:21:2: error: need ‘typename’ before ‘std::vector<a_type>::iterator’ because ‘std::vector<a_type>’ is a dependent scope
  vector< a_type >::iterator vec1_pre = vec1.begin();
  ^
my_vector.h:21:29: error: expected ‘;’ before ‘vec1_pre’
  vector< a_type >::iterator vec1_pre = vec1.begin();
                             ^
my_vector.h:22:2: error: need ‘typename’ before ‘std::vector<a_type>::iterator’ because ‘std::vector<a_type>’ is a dependent scope
  vector< a_type >::iterator vec1_pos = vec1.begin();
  ^
my_vector.h:22:29: error: expected ‘;’ before ‘vec1_pos’
  vector< a_type >::iterator vec1_pos = vec1.begin();
                             ^
my_vector.h:24:38: error: ‘vec1_pos’ was not declared in this scope
  while(ind_pt != index_list.end() && vec1_pos != vec1.end())
                                      ^
my_vector.h:34:6: error: ‘vec1_pre’ was not declared in this scope
    *(vec1_pre) = *(vec1_pos);
      ^
my_vector.h:45:8: error: ‘vec1_pos’ was not declared in this scope
  while(vec1_pos != vec1.end())
        ^
my_vector.h:47:5: error: ‘vec1_pre’ was not declared in this scope
   *(vec1_pre) = *(vec1_pos);
     ^
my_vector.h:54:8: error: ‘vec1_pre’ was not declared in this scope
  while(vec1_pre != vec1.end() )

could anyone help me solve this?

question from:https://stackoverflow.com/questions/22874535/dependent-scope-need-typename-in-front

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The compiler says

my_vector.h:21:2: error: need ‘typename’ before ‘std::vector::iterator’ because ‘std::vector’ is a dependent scope

So you need to write

typename vector< a_type >::iterator vec1_pre = vec1.begin();
typename vector< a_type >::iterator vec1_pos = vec1.begin();

See Where and why do I have to put the "template" and "typename" keywords? for the reasons behind it.

One last remark: In C++11 you can use auto and don't have to think anymore:

auto vec1_pre = vec1.begin();
auto vec1_pos = vec1.begin();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...