2014-04-04 6 views
7

मैं टेम्पलेट के रूप में एक टेम्पलेट बनाना चाहता हूँ। मैं वेक्टर 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()) 

किसी ने मुझे इस के समाधान में मदद कर सकता है?

+0

त्रुटि संदेश आपको बताता है कि आप क्या जरूरत है। टाइपलाइन कीवर्ड को उन पंक्तियों पर रखें –

+1

उन पंक्तियों के सामने 'टाइपनाम' डालने का प्रयास करें। – ooga

+0

@ooga ठीक है। बढ़िया, यह काम करता है। यह आश्चर्यजनक है। लेकिन क्या आप मुझे बता सकते हैं कि मुझे लाइनों के सामने 'टाइपनाम' रखना चाहिए? – tqjustc

उत्तर

10

संकलक का कहना है

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

तो तुम लिखने के लिए

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? देखें जरूरत है।

एक आखिरी टिप्पणी: सी ++ 11 में आप auto उपयोग कर सकते हैं और अब लगता है की जरूरत नहीं है:

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

मुझे नहीं लगता कि मुझे इस लाइन की आवश्यकता है 'टाइपनाम वेक्टर < int > :: iterator ind_pt = index_list.begin(); ' क्या आप इसे हटा सकते हैं? धन्यवाद! – tqjustc

+0

पूरी तरह से सही :) – Danvil

+0

ग्रेट। धन्यवाद ! – tqjustc

संबंधित मुद्दे