C ++错误:转换为非标量类型 [英] C++ Error: Conversion to Non-Scalar Type

查看:920
本文介绍了C ++错误:转换为非标量类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码段中似​​乎出现了一个特殊的错误(忽略多余的头文件和空白的main函数,我只是想将此问题隔离到一个可编译的.cpp文件中,以便在此处发布).它表示从"[我定义的某些类型]"到非标量"[我定义的某些类型]"的错误转换.

I seem to be having a peculiar error in the following code segment (ignore the excess header files and the blank main function, I just wanted to isolate this problem into a compileable .cpp file for posting here). It says error conversion from '[some type I defined]' to non-scalar type '[some type I defined]'.

此特定示例中的代码假定将一组字符串列表作为一个输入参数(命名输入),并将对字符串列表的引用作为另一个输入参数(命名输出),并计算最长的公共前缀列表输入中的列表中的字符串,并将结果存储到输出中.

The code in this particular example is supposed to take a set of list of strings as one input parameter (named input), and a reference to a list of strings as another (named output) and calculate the longest common prefix list of strings from among the lists in input and store the result into output.

编译器错误消息(也在相应行中作为注释包括:

The compiler error message (also included as a comment in the corresponding line is this:

lcp.cpp:28:47:错误:请求从"std::list<std::basic_string<char> >::const_iterator {aka std::_List_const_iterator<std::basic_string<char> >}"到非标量类型"std::list<std::basic_string<char> >::iterator {aka std::_List_iterator<std::basic_string<char> >}"的转换

lcp.cpp:28:47: error: conversion from ‘std::list<std::basic_string<char> >::const_iterator {aka std::_List_const_iterator<std::basic_string<char> >}’ to non-scalar type ‘std::list<std::basic_string<char> >::iterator {aka std::_List_iterator<std::basic_string<char> >}’ requested

这是实际程序:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <list>

using namespace std;

void getLongestCommonPrefix(set <list <string> > & input, list <string> & output)
{

    set <list <string> > :: iterator it = input.begin();

    output = *it;

    for(; it != input.end(); ++it)
    {
        if(output.size() > (*it).size())
        {
            list <string> :: iterator it1 = output.begin();
            advance(it1, (*it).size()-1);
            output.erase(it1, output.end());
        }

        list <string> :: iterator it1 = output.begin();
        list <string> :: iterator it2 = (*it).begin();  //lcp.cpp:28:47: error: conv    ersion from ‘std::list<std::basic_string<char> >::const_iterator {aka     std::_List_const_iterator<std::basic_string<char> >}’ to non-scalar type     ‘std::list<std::basic_string<char> >::iterator {aka     std::_List_iterator<std::basic_string<char> >}’ requested

        for(; it1 != output.end(); ++it1,++it2)
        {
            if(*it1 != *it2)
                break;
        }

        output.erase(it1, output.end());

        if(!output.size())
            return;
    }
}     

int main()
{
    return 0;
}

在这里,我很乐意听取专家关于此类错误发生的原因和时间以及解决方法的信息.

I would love to hear from the experts here as to why and when this sort of error happens and what the work-around could be.

推荐答案

自C ++ 11起,std::set没有任何非常数迭代器.当你这样说:

Since C++11, std::set has no non-constant iterator. When you say this:

(*it).begin();

您要取消引用常量迭代器以获取常量对象,并在该对象上调用begin(),这将为您提供另一个常量迭代器,因为该对象是常量.然后,您尝试将此常量迭代器存储到非常量迭代器中,从而导致错误.

you're dereferencing a constant iterator to get a constant object and calling begin() on that object, which gives you another constant iterator since the object is constant. You then try to store this constant iterator into a non-constant iterator, hence the error.

这篇关于C ++错误:转换为非标量类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆