C++ 依赖范围;前面需要typename;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22874535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:15:04  来源:igfitidea点击:

dependent scope; need typename in front;

c++templatesvector

提问by tqjustc

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.

我想创建一个模板如下。我想从 vector 中删除项目列表vec1。我要删除的项目的索引存储在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?

谁能帮我解决这个问题?

回答by Danvil

The compiler says

编译器说

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

my_vector.h:21:2: 错误:在“std::vector::iterator”之前需要“typename”,因为“std::vector”是一个依赖范围

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 autoand don't have to think anymore:

最后一句话:在 C++11 中,你可以使用auto,不用再想了:

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