为什么不这个complile ???常见的麻烦...... [英] why won't this complile??? const troubles...

查看:138
本文介绍了为什么不这个complile ???常见的麻烦......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不想编译....


类图片:public std :: list< element>

{

元素getElement(key k)const

{

image :: iterator iter;

for(iter = begin() ; iter!= end(); ++ iter)

{

element& elem(* iter);

if(Key = = k)

{

返回(elem);

}

}

throw(找不到);

}

};


我认为这不是原因工作是由于



方法的常规声明。

那我该怎么办?

解决方案

JustSomeGuy写道:

这不想编译....


总是发布完整的错误消息。

类图像:public std :: list< element>
{
元素getElement(key k)const
{
image :: iterator iter;
for(iter = begin(); iter!= end(); ++ iter)
{
element& elem(* iter);
if(Key == k)
{
return(elem);
}
}
抛出(未找到);
}
};

我认为这是行不通的原因是由于方法的const声明。




这是由于你试图在const上使用非const-iterator

容器。使用const_iterator而不是迭代器。


JustSomeGuy写道:

这个不想编译.. ..

类图像:public std :: list< element>
元素getElement(key k)const
{
image :: iterator iter;
for(iter = begin(); iter!= end(); ++ iter)
{
element& elem(* iter);
if(Key) == k)
{
返回(elem);
}
}
抛出(未找到);
}
}}

我认为这不起作用的原因是由于
方法的const声明。
那我该怎么办? / blockquote>


不要继承std :: list<> ;.


通常,只会继承来覆盖虚方法。 STL不使用任何

虚拟广告。


如果很多客户使用图像类,他们可能会开始玩所有的

std :: list<>的不同函数提供。当时间到了
用其他东西替换列表时,那些客户会抵制那个

的变化。库类(例如std :: list<>)应具有非常宽的

接口,但特定于应用程序的类必须非常窄。


BTW实际修复是image :: const_iterator。


-

Phlip
http://industrialxp.org/community/bi...UserInterfaces


Phlip写道:

不要继承std :: list<> ;.

一般来说,仅继承以覆盖虚拟方法。 STL不使用任何
虚拟。




之前我问过一个关于继承std :: priority_queue<>的问题。它好像
似乎priority_queue被设计为继承(因为它有

受保护的成员),即使它没有虚拟。


继承有很多理由,即使某些东西没有虚拟,

如添加新​​行为,即使它没有修改现有的

(注意我正在使用私人继承。)


毯子禁令可能不是一件好事。


this doesn''t want to compile....

class image : public std::list<element>
{
element getElement(key k) const
{
image::iterator iter;
for (iter=begin(); iter != end(); ++iter)
{
element &elem(*iter);
if (Key == k)
{
return(elem);
}
}
throw("Not found");
}
};

I think the reason this isn''t working is due to the const declaration of
the
method.
So what am I to do?

解决方案

JustSomeGuy wrote:

this doesn''t want to compile....
Always post the full error message.
class image : public std::list<element>
{
element getElement(key k) const
{
image::iterator iter;
for (iter=begin(); iter != end(); ++iter)
{
element &elem(*iter);
if (Key == k)
{
return(elem);
}
}
throw("Not found");
}
};

I think the reason this isn''t working is due to the const declaration
of the method.



It is due to you trying to use a non-const-iterator on a const
container. Use const_iterator instead of iterator.


JustSomeGuy wrote:

this doesn''t want to compile....

class image : public std::list<element>
{
element getElement(key k) const
{
image::iterator iter;
for (iter=begin(); iter != end(); ++iter)
{
element &elem(*iter);
if (Key == k)
{
return(elem);
}
}
throw("Not found");
}
};

I think the reason this isn''t working is due to the const declaration of
the
method.
So what am I to do?



Don''t inherit std::list<>.

Generally, only inherit to override a virtual method. STL does not use any
virtuals.

If many clients used the image class, they might start playing with all the
different functions which std::list<> provides. When the time came to
replace the list with something else, those clients would resist that
change. Library classes, such as std::list<>, should have very wide
interfaces, but application-specific classes must be very narrow.

BTW the actual fix is image::const_iterator.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces



Phlip wrote:

Don''t inherit std::list<>.

Generally, only inherit to override a virtual method. STL does not use any
virtuals.



Earlier I asked a question about inheriting std::priority_queue<>. It
seems that priority_queue is designed to be inherited (as it has
protected members), even though it has no virtuals.

There are many reasons to inherit, even if something has no virtuals,
such as adding new behavior, even though it doesn''t modify existing
(Note that I was using private inheritance).

Blanket bans may not be a good thing.


这篇关于为什么不这个complile ???常见的麻烦......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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