几个简单的代码行中有两个VS2005 C ++编译器错误... [英] Two VS2005 C++ compiler bugs in few simple lines of code...

查看:60
本文介绍了几个简单的代码行中有两个VS2005 C ++编译器错误...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个相对简单的代码片段:

#include< memory>


class Base {

public :

Base();

virtual~Base();

virtual void f(int a,char const * name);

};


class Sub:public Base {

public:

Sub();

虚拟~Sub();

// **问题1 **:错误警告

虚拟空白f(int a,char const * const姓名);

};


class其他{//注意:忘了指定基类

public:

其他();

虚拟〜其他();

虚拟空虚f(int a,char const * const name);

};


std :: auto_ptr< Basefactory()

{

// **问题2 ** :这应该是编译错误

return std :: auto_ptr< Base>(new Other());

}

**问题1 **:VS2005报告以下警告:

警告2警告C4301:''Sub :: f' :仅覆盖虚函数

与''Base :: f''的区别在于const / volatile限定符

c:\ dev \ivec_dev \ivec\render \\ image\imagelogger.cpp 23


但是增加的顶级 const不是函数签名的一部分,

因此代码完全合法。

**问题2 **:VS2005生成无效代码

这应该触发编译错误,因为Other是

而不是Base的子类。不知何故,这个滑倒,

和无效代码生成(指针值不知何故

变得乱码......)。 (编译器或库bug?)

如果在即将推出的服务包中找到修复好的话,可以运气好吗?


-
http://ivan.vecerina.com/contact/?subject=NG_POST < ; - 电子邮件联系表格

Here''s a relatively simple code snippet:
#include <memory>

class Base {
public:
Base();
virtual ~Base();
virtual void f(int a, char const* name);
};

class Sub : public Base {
public:
Sub();
virtual ~Sub();
// **PROBLEM 1** : incorrect warning
virtual void f(int a, char const* const name);
};

class Other { //NB: forgot to specify base class
public:
Other();
virtual ~Other();
virtual void f(int a, char const* const name);
};

std::auto_ptr<Basefactory()
{
// **PROBLEM 2** : this should be a compilation error
return std::auto_ptr<Base>( new Other() );
}
**PROBLEM 1** : VS2005 reports the following warning:
Warning 2 warning C4301: ''Sub::f'': overriding virtual function only
differs from ''Base::f'' by const/volatile qualifier
c:\dev\ivec_dev\ivec\render\image\imagelogger.cpp 23

But the added "top-level" const is not part of the function''s signature,
so the code is perfectly legit.
**PROBLEM 2** : VS2005 generates invalid code
This should trigger a compilation error, because Other is
not a subclass of Base. Somehow this slips through,
and invalid code is generated (the pointer value somehow
gets garbled...). (compiler or library bug??)
Any luck to see those fixed in the upcoming service pack?

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form

推荐答案

Ivan Vecerina写道:
Ivan Vecerina wrote:

这是一个相对简单的代码片段:

[...]

**问题2 **:VS2005生成无效代码

这个应该触发编译错误,因为Other是

不是Base的子类。不知何故,这个滑倒,

和无效代码生成(指针值不知何故

变得乱码......)。 (编译器或库bug?)
Here''s a relatively simple code snippet:
[...]
**PROBLEM 2** : VS2005 generates invalid code
This should trigger a compilation error, because Other is
not a subclass of Base. Somehow this slips through,
and invalid code is generated (the pointer value somehow
gets garbled...). (compiler or library bug??)



似乎是一个库bug。 (?)


在auto_ptr的内存头文件中有一个构造函数

构造一个接受void指针的auto_ptr_ref对象。由于

void *匹配所有指针,它接受所有指针并转换回

到auto_ptr对象的指针类型。


更改:auto_ptr_ref(void * _Right)进入

auto_ptr_ref(_Ty * _Right)修复了该错误。虽然我不知道这个改变可能会对其他代码产生什么影响,以及为什么有一个非显式的

构造函数,该代理类参数接受void指针。 (???)

Seems to be a library bug. (?)

There''s a constructor in the memory header file for auto_ptr
constructing a auto_ptr_ref object which accepts a void pointer. Since
void* matches all pointers it accepts all pointers and is converted back
to the pointer type of the auto_ptr object.

changing: auto_ptr_ref(void *_Right) into
auto_ptr_ref(_Ty *_Right) fixes the bug. Though I don''t know what impact
this change may have on other code and why there''s a non explicit
constructor with that proxy class argument accepting void pointers. (???)


如果在即将推出的服务包中看到这些内容,有什么好运的吗?
Any luck to see those fixed in the upcoming service pack?



不知道它是否已经在VS2005的SP1 Beta中得到修复。我会给它

a尝试并报告错误。


Andre

Don''t know if it''s already fixed in the SP1 Beta of VS2005. I''ll give it
a try and will report the bug.

Andre


Ivan Vecerina写道:
Ivan Vecerina wrote:

这里是一个相对简单的代码片段:
Here''s a relatively simple code snippet:


>

如果在即将推出的服务包中找到修复好的话,还能运气好吗?
>
Any luck to see those fixed in the upcoming service pack?



这在VC 2005 SP1决赛中没有变化。


-cd

This is unchanged in VC 2005 SP1 final.

-cd



我不知道为什么你抱怨第一个警告。它确实说到了你所遇到的问题。 ''MSDN页面上的摘要''

http://msdn2.microsoft.com/en-us/library/bby1ahfc.aspx )显示案件

几乎与你的相同。 C ++语言具有处理const int的

的危险特性。和int形式参数作为不同的类型。

但实际上这些参数的行为几乎相同(与指针不同:

int * vs. const int *)。该警告应该可以节省你很多时间

调查为什么你打算在

派生类中覆盖的函数没有被覆盖。我可以向你保证,这是一个很好的帮助。

我在使用VC ++ 6.0时遇到了这个问题。


-

alexcohn

----------------------------------------- -------------------------------

发表于 http://www.codecomments.com

------------ -------------------------------------------------- ----------


I don''t know why you complain about the first warning. It does speak
exactly about the problem you''ve got. ''summary on MSDN page''
(http://msdn2.microsoft.com/en-us/library/bby1ahfc.aspx) shows the case
almost identical to yours. The C++ language has a dangerous feature of
treating "const int" and "int" formal parameters as different types.
But actually such parameters behave almost the same (unlike pointers:
int* vs. const int*). The warning is supposed to save you lots of hours
of investigation why a function that you intended to override in a
derived class was not overridden. I can assure you, it''s a great help.
I experienced this problem with VC++ 6.0.

--
alexcohn
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------


这篇关于几个简单的代码行中有两个VS2005 C ++编译器错误...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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