Error:conversion from ‘A’ to non-scalar type ‘B’ requested

10:44:00 AM 0 Comments

Error:conversion from ‘A’ to non-scalar type ‘B’ requested

今天遇到了下面的问题:
error: conversion from ‘std::map, std::map, std::basic_string > >::const_iterator’ to non-scalar type ‘TextColors::ColorStylesIter’ requested
std::string TextColors::GetTextColor(std::string &styleName, std::string &textKind)const
{
    ColorStylesIter colorStylesIter = colorStyles.find(styleName);
    ……………………
}
在TextColors类的头文件中有:
typedef std::map ColorStyles;
typedef std::map::iterator ColorStylesIter;
错误的原因是:该成员函数(GetTextColor)是const成员函数,所以colorStyles.find(styleName)的返回值为const_iterator.
修改方法:在TextColors类的头文件中添加:
typedef std::map::const_iterator ColorStylesConstIter;
在GetTextColor的实现中使用ColorStylesConstIter;

下面的解答来自:http://topic.csdn.net/u/20110406/09/c17252ce-d022-404b-bcd0-90e10a4b7362.html
1.)
class base
{};
int main()
{
base b;//这个实例和下面的产生实例的方法的区别是什么阿 ?
base *b2 = new base();//
return 0;
}
2)
这里面的这个翻译 non-scalar type怎么翻译阿 ?
base.cpp:11: error: conversion from ‘base*’ to non-scalar type ‘base’ requested
答:
scalar在物理或者数学里面就是“标量”(即一个纯粹的数值)的意思。non-scalar type就是“非标量的数据类型”。
指针(base*)只是一个int,因此就是标量。
对象(base)不是一个简单的数值,因此就是非标量。
“conversion from ‘base*’ to non-scalar type ‘base’ requested”的意思:
要求提供从‘base*’到非标量数据类型‘base'的转换。

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard.