奇怪的错误 [英] weird error

查看:85
本文介绍了奇怪的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(更改文件和类名以保护无辜者)


g ++ -Wall -c file.cpp:在成员函数`std :: string

Some_Class :: get_member(const std :: string&)const'':

file.cpp:46:传递`const std :: map< std :: string,

std :: string,std :: less< std :: string>,std :: allocator< std :: pair< const

std :: string,std :: string> > >''作为`_Tp&的'this'参数std :: map< _Key,

_Tp,_Compare,_ Alloc> :: operator [](const _Key&)[with _Key = std :: string,

_Tp = std :: string,_Compare = std :: less< std :: string>,_ Alloc =

std :: allocator< std :: pair< const std :: string,std :: string> >]''丢弃

限定符


我无法做出这个错误的正面或反面。我猜它在某些const对象上调用一个非const函数时会有一些东西。任何想法?


函数SomeClass :: get_member是:


std :: string SomeClass :: get_member(const std :: string& ;姓名)const

{

返回_member [姓名];

}


其中_成员是一个std :: map< std :: string,std :: string> ;.

(file and class names changed to protect the innocent)

g++ -Wall -c file.cpp: In member function `std::string
Some_Class::get_member(const std::string&) const'':
file.cpp:46: passing `const std::map<std::string,
std::string, std::less<std::string>, std::allocator<std::pair<const
std::string, std::string> > >'' as `this'' argument of `_Tp& std::map<_Key,
_Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = std::string,
_Tp = std::string, _Compare = std::less<std::string>, _Alloc =
std::allocator<std::pair<const std::string, std::string> >]'' discards
qualifiers

I can''t make heads or tails of this error. I''m guessing it has something to
do with calling a non-const function on some const object. Any ideas?

The function SomeClass::get_member is:

std::string SomeClass::get_member(const std::string &name) const
{
return _member[name];
}

where _member is a std::map<std::string, std::string>.

推荐答案

" Joe Laughlin" <乔*************** @ boeing.com>写道...
"Joe Laughlin" <Jo***************@boeing.com> wrote...
(文件和类名改变以保护无辜者)

g ++ -Wall -c file.cpp:在成员函数`std :: string
Some_Class :: get_member(const std :: string&)const'':
file.cpp:46:传递`const std :: map< std :: string,
std :: string, std :: less< std :: string>,std :: allocator< std :: pair< const
std :: string,std :: string> > >''作为`_Tp&
std :: map< _Key,
_Tp,_Compare,_Alloc> :: operator [](const _Key&)的'this'参数[与_Key =
std :: string,
_Tp = std :: string,_Compare = std :: less< std :: string>,_ Alloc =
std :: allocator< std :: pair< const std :: string,std :: string> >]''丢弃
限定符

我无法做出这个错误的正面或反面。我猜它在某些const对象上调用非const函数时会有什么作用。任何想法?

函数SomeClass :: get_member是:

std :: string SomeClass :: get_member(const std :: string& name)const
{
返回_member [name];
}
其中_member是std :: map< std :: string,std :: string>。
(file and class names changed to protect the innocent)

g++ -Wall -c file.cpp: In member function `std::string
Some_Class::get_member(const std::string&) const'':
file.cpp:46: passing `const std::map<std::string,
std::string, std::less<std::string>, std::allocator<std::pair<const
std::string, std::string> > >'' as `this'' argument of `_Tp&
std::map<_Key,
_Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key =
std::string,
_Tp = std::string, _Compare = std::less<std::string>, _Alloc =
std::allocator<std::pair<const std::string, std::string> >]'' discards
qualifiers

I can''t make heads or tails of this error. I''m guessing it has something
to
do with calling a non-const function on some const object. Any ideas?

The function SomeClass::get_member is:

std::string SomeClass::get_member(const std::string &name) const
{
return _member[name];
}

where _member is a std::map<std::string, std::string>.




您声明了此成员函数(''get_member'')" const"。这意味着

对象(除了''mutable''之外的所有数据成员)都不会改变
。 ''map''模板的索引操作符是_non-const_函数。

I.e.它不能被称为const映射。它必须是非const的,因为它可能会插入一个新的对象,如果它还没有那么。


你需要和平事实上它可能会在你的_member中插入另一个

对象(并声明''_member'''可变的')或使用其他

方式。看来你的函数_assumes_在地图中有一个元素



键''name''。如果没有怎么办?


Victor



You declared this member function (''get_member'') "const". That means that
the object (and all its data member except ''mutable'' ones) are not going to
change. Indexing operator for ''map'' template is a _non-const_ function.
I.e. it cannot be called for a const map. It has to be non-const because
it may insert a new object if it''s not there yet.

You need to either make peace with the fact that it may insert another
object into your _member (and declare ''_member'' "mutable") or use some other
way. It seems that your function _assumes_ that there is an element with
the
key ''name'' in the map. What if there isn''t?

Victor


Joe Laughlin写道:
Joe Laughlin wrote:
(file和类名改变以保护无辜者)

g ++ -Wall -c file.cpp:在成员函数`std :: string
Some_Class :: get_member(const std :: string&) const'':
file.cpp:46:传递`const std :: map< std :: string,
std :: string,std :: less< std :: string>,std :: allocator< std :: pair< const
std :: string,std :: string> > >''作为`_Tp&的'this'参数std :: map< _Key,
_Tp,_Compare,_ Alloc> :: operator [](const _Key&)[with _Key = std :: string,
_Tp = std :: string,_Compare = std :: less< std :: string>,_ Alloc =
std :: allocator< std :: pair< const std :: string,std :: string> >]''丢弃
限定符

我无法做出这个错误的正面或反面。我猜它在某些const对象上调用非const函数时会有所作为。任何想法?

函数SomeClass :: get_member是:

std :: string SomeClass :: get_member(const std :: string& name)const
{
返回_member [姓名];


你的函数被声明为const,但你无法保证地图的

下标运算符不会改变''name''的值。用

换句话说:在const成员函数中,你只能使用其他const

函数(关于你自己的成员)。在上面,如果''name''确实
$ _ $ b在_member中不存在,*它将被创建*。你不能在const函数中做



}
(file and class names changed to protect the innocent)

g++ -Wall -c file.cpp: In member function `std::string
Some_Class::get_member(const std::string&) const'':
file.cpp:46: passing `const std::map<std::string,
std::string, std::less<std::string>, std::allocator<std::pair<const
std::string, std::string> > >'' as `this'' argument of `_Tp& std::map<_Key,
_Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = std::string,
_Tp = std::string, _Compare = std::less<std::string>, _Alloc =
std::allocator<std::pair<const std::string, std::string> >]'' discards
qualifiers

I can''t make heads or tails of this error. I''m guessing it has something to
do with calling a non-const function on some const object. Any ideas?

The function SomeClass::get_member is:

std::string SomeClass::get_member(const std::string &name) const
{
return _member[name];
Your function is declared const, but you cannot guarantee that the
subscript operator of the map doesn''t change the value at ''name''. In
other words: In a const member function you can only use other const
functions (in regard to your own members). In the above, if ''name'' does
not exist in _member, *it will be created*. You are not allowed to do
that in a const function.
}




试试这个:


std :: string SomeClass :: get_member(std :: string const& name)const

{

_member.const_iterator it ;

it = _member.find(name);

if(it!= _member.end())

_return it->第二个;

其他

< ...> //无论你想做什么,如果''名字''不存在......

}


占用更多空间,但它的确有效。在大多数编译器上我应该

认为它也同样有效。


-

/ Brian Riis



try this:

std::string SomeClass::get_member(std::string const & name) const
{
_member.const_iterator it;
it = _member.find(name);
if(it != _member.end())
_return it->second;
else
<...> // Whatever you want to do, if ''name'' isn''t there...
}

Takes up a bit more space, but it works. On most compilers I should
think it is as efficient too.

--
/Brian Riis


Brian Riis写道:
Brian Riis wrote:
Joe Laughlin写道:
Joe Laughlin wrote:
(文件和类名改为保护无辜者)

g ++ -Wall -c file.cpp:在成员函数`std :: string
Some_Class :: get_member(const std :: string&)const'':
file.cpp:46:传递`const std :: map< std :: string,
std :: string,std :: less< std :: string>,
std :: allocator< std :: pair< const std: :string,
std :: string> > >''作为`_Tp&
std :: map< _Key,_Tp,_Compare,
_Alloc> :: operator [](const _Key&)的'this'参数[与_Key =
std :: string,_比较=
std :: less< std :: string>,_ Alloc =
:: string,std :: string>
(file and class names changed to protect the innocent)

g++ -Wall -c file.cpp: In member function `std::string
Some_Class::get_member(const std::string&) const'':
file.cpp:46: passing `const std::map<std::string,
std::string, std::less<std::string>,
std::allocator<std::pair<const std::string,
std::string> > >'' as `this'' argument of `_Tp&
std::map<_Key, _Tp, _Compare,
_Alloc>::operator[](const _Key&) [with _Key =
std::string, _Tp = std::string, _Compare =
std::less<std::string>, _Alloc =
std::allocator<std::pair<const std::string, std::string>
>]''丢弃限定符
>]'' discards qualifiers



我不能对此错误做出正面或反面。我猜测它与在一些const对象上调用非const函数有关。任何想法?

函数SomeClass :: get_member是:

std :: string SomeClass :: get_member(const std :: string
&name;)const {
返回_member [name];



I can''t make heads or tails of this error. I''m guessing
it has something to do with calling a non-const function
on some const object. Any ideas?

The function SomeClass::get_member is:

std::string SomeClass::get_member(const std::string
&name) const {
return _member[name];



你的函数被声明为const,但你无法保证
地图的下标运算符不会改变
'name''的价值。换句话说:在const成员
函数中,您只能使用其他const函数(关于您自己的成员)。在上面,如果'_ name''在_member中不存在,*它将被创建*。你不能在const函数中这样做。



Your function is declared const, but you cannot guarantee
that the subscript operator of the map doesn''t change the
value at ''name''. In other words: In a const member
function you can only use other const functions (in
regard to your own members). In the above, if ''name'' does
not exist in _member, *it will be created*. You are not
allowed to do that in a const function.

}



试试这个:

std :: string SomeClass :: get_member(std :: string const&
name)const {
_member.const_iterator it;
it = _member.find(name);
if(it!= _member.end())
_return it-> second;
else
< ...> //无论你想做什么,如果''名字''不在那里......}

占用更多空间,但它有效。在大多数
编译器上,我认为它也同样有效。



try this:

std::string SomeClass::get_member(std::string const &
name) const {
_member.const_iterator it;
it = _member.find(name);
if(it != _member.end())
_return it->second;
else
<...> // Whatever you want to do, if ''name'' isn''t
there... }

Takes up a bit more space, but it works. On most
compilers I should think it is as efficient too.




谢谢你们。


是:

_member.const_iterator it;

合法?编译器不喜欢那条线,但确实如此:

std :: map< std :: string,std :: string> :: const_iterator it;


Joe



Thanks you guys.

Is:
_member.const_iterator it;
legal? The compiler didn''t like that line, but it did like:
std::map<std::string, std::string>::const_iterator it;

Joe


这篇关于奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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