我对这段代码有疑问:
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
这可能是一个简单的问题,无论如何,对我来说不是:)。
非常感谢。
最佳答案
因为您的函数是常量,所以您只能常量访问类的 this 指针。结果是不断地访问您的 vector 。您需要从 vector 中获取一个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);
}
}
关于包含用户定义类型指针的 C++ vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7504396/