简体   繁体   English

包含用户定义类型指针的C ++向量

[英]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): 我正在为项目使用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

This may be simple problem, anyway, not to me:). 这可能是一个简单的问题,无论如何,不​​是我:)。

Great thanks in advance. 非常感谢提前。

Because your function is constant, you only have constant access to the this pointer of your class. 因为您的函数是常量,所以您只能持续访问类的this指针。 The results in a constant access to your vector. 这样可以持续访问您的向量。 You need to get a const_iterator from the vector. 你需要从向量中获取一个const_iterator

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM