【问题标题】:dependent scope; need typename in front;依赖范围;前面需要typename;
【发布时间】:2014-05-17 11:02:16
【问题描述】:

我想创建一个模板如下。我想从向量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;
}

但它会返回很多错误:

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() )

谁能帮我解决这个问题?

【问题讨论】:

  • 错误信息告诉你你需要什么。将 typename 关键字放在这些行上
  • 尝试将typename 放在这些行的前面。
  • @ooga 好的。太好了,它有效。太棒了。但是你能告诉我什么时候应该把'typename'放在行前吗?
  • @tqjustc,当它是一个依赖于模板参数的声明时。
  • 当然可以告诉你,wait, someone already did.

标签: c++ templates vector


【解决方案1】:

编译器说

my_vector.h:21:2: 错误:之前需要‘typename’ 'std::vector::iterator' 因为'std::vector' 是一个 依赖范围

所以你需要写

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

查看Where and why do I have to put the "template" and "typename" keywords?了解背后的原因。

最后一句话:在 C++11 中你可以使用 auto 并且不必再考虑了:

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

【讨论】:

    猜你喜欢
    • 2017-02-15
    • 2011-01-21
    • 2016-08-12
    • 2020-09-11
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    相关资源
    最近更新 更多