2011-09-21 123 views
3

我有這個代碼的問題:C++矢量

struct document_type_content 
{ 
    long long m_ID; 
    QString m_type_name; 
}; 

std::vector<QString> document_type::get_fields_by_name(e_doc_type) const 
{ 
    std::vector<QString> tmp; 
    std::vector<document_type_content*>::iterator it = m_table_data.begin(), 
      it_end = m_table_data.end(); 

    for (; it != it_end; ++it) { 
     document_type_content* cnt = *it; 
     tmp.push_back(cnt->m_type_name); 
    } 
} 

我用QtCreator的項目和它給了我下面的錯誤(系,其中迭代器正在初始化):

error: conversion from '__gnu_cxx::__normal_iterator<document_type_content* const*, std::vector<document_type_content*, std::allocator<document_type_content*> > >' to non-scalar type '__gnu_cxx::__normal_iterator<document_type_content**, std::vector<document_type_content*, std::allocator<document_type_content*> > >' requested 

這可能是一個簡單的問題,反正,不是我:)。

非常感謝提前。

回答

5

因爲你的函數是不變的,所以你只能持續訪問你的類的這個指針。持續訪問您的矢量的結果。您需要從矢量中獲得const_iterator

這應該做的伎倆:

std::vector<QString> document_type::get_fields_by_name(e_doc_type) const 
{ 
    std::vector<QString> tmp; 
    std::vector<document_type_content*>::const_iterator it = m_table_data.begin(), 
      it_end = m_table_data.end(); 

    for (; it != it_end; ++it) { 
     document_type_content* cnt = *it; 
     tmp.push_back(cnt->m_type_name); 
    } 
} 
+1

喔,非常感謝你! – Dehumanizer