Dehumanizer
Dehumanizer

Reputation: 284

C++ vector containing pointers of user-defined types

I have a problem with this code:

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);
    }
}

I'm using QtCreator for the project and it's gave me the following error(for lines, where the iterator is being initialized):

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

This may be simple problem, anyway, not to me:).

Great thanks in advance.

Upvotes: 2

Views: 668

Answers (1)

Fox32
Fox32

Reputation: 13560

Because your function is constant, you only have constant access to the this pointer of your class. The results in a constant access to your vector. You need to get a const_iterator from the vector.

This should do the trick:

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);
    }
}

Upvotes: 5

Related Questions