无法在向量中为指针写入0。 [英] Cannot write 0 for pointer in a vector.

查看:61
本文介绍了无法在向量中为指针写入0。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,


我发现该计划


#include< vector>


using namespace std;


int main()

{vector< int *> v(2,0);

返回0;

}


不使用g ++ 4.0.2编译。它说(包括相当多的STL错误

消息):错误:从错误转换无效:从

const int到int *"的无效转换。这让我试着用v(2,NULL)代替v(2,0),现在它编译了




然而,我似乎含糊不清请记住,在C ++中,NULL应该#defined为0

,那么为什么会出现这个错误呢?一个编译器错误地抱怨这个吗?


最好,

克里斯

Dear all,

I found out that the program

#include<vector>

using namespace std;

int main()
{ vector<int*> v(2,0);
return 0;
}

does not compile using g++ 4.0.2. It says (including quite a of STL-error
message): "error: invalid conversion from error: invalid conversion from
const int to int*". This made me try to replace v(2,0) by v(2,NULL) and now
it compiles.

However, I seem to vaguely remember that NULL should be #defined to be 0
in C++, so why this error? Is the a compiler wrong to complain about this?

Best,
Chris

推荐答案

* Chris Dams:
* Chris Dams:

我发现程序

#include< vector>

使用命名空间std ;

int main()
{vector< int *> v(2,0);
返回0;


不使用g ++ 4.0.2编译。它说(包括相当多的STL错误消息):错误:错误的无效转换:从
const int到int *的无效转换。这让我尝试用v(2,NULL)代替v(2,0),现在它编译了。

然而,我似乎依稀记得NULL应该是#defined到在C ++中为0
,为什么会出现这个错误?一个编译器错误地抱怨这个吗?

I found out that the program

#include<vector>

using namespace std;

int main()
{ vector<int*> v(2,0);
return 0;
}

does not compile using g++ 4.0.2. It says (including quite a of STL-error
message): "error: invalid conversion from error: invalid conversion from
const int to int*". This made me try to replace v(2,0) by v(2,NULL) and now
it compiles.

However, I seem to vaguely remember that NULL should be #defined to be 0
in C++, so why this error? Is the a compiler wrong to complain about this?




你所反对的是模板化构造函数的选择

with two迭代器,而不是具有大小和默认值

值的构造函数(我_think_那是'因为默认值是通过引用传递给

到const,但有人正确我不是这样的。


第23.1.1 / 9段定义了模板化构造函数的效果


template< class Iter>

X(Iter first,Iter last,Allocator const& a = Allocator())


as


X(

static_cast< typename X :: size_type>(第一个),

static_cast< typename X :: value_type>(last),

a




当Iter是一个整体类型时,就像你的情况一样。


然而,一个自然的解释是这适用于实现,

不作为客户端代码转换,并且在实现中

编译时间0已转换为运行时间参数类型

''int'',不能转换为''int *''。


总结一下,编译器似乎是对于文字0,<​​br />
的情况是正确的,对于NULL的情况,可能是这个编译器的NULL是

定义为0L(例如),它'允许是,然后两个

参数不是同一类型,模板构造uctor不是选择
,如果是这样的话就意味着编译器是正确的。


如果我的分析是正确的,那意味着这个几乎无足轻重的小块代码有未定义的行为(不同的编译器可以

然后自由选择接受或拒绝NULL情况,具体取决于

定义为NULL)。


-

答:因为它弄乱了人们通常阅读文本的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?



What you''re up against is the selection of the templated constructor
with two iterators, instead of the constructor with a size and a default
value (I _think_ that''s because the default value is passed by reference
to const, but someone correct me if that''s not the case).

Paragraph 23.1.1/9 defines the effect of the templated constructor

template< class Iter >
X( Iter first, Iter last, Allocator const& a = Allocator() )

as

X(
static_cast<typename X::size_type>( first ),
static_cast<typename X::value_type>( last ),
a
)

when Iter is an integral type, as it is in your case.

However a natural reading is that this applies to the implementation,
not as a client code transformation, and in the implementation the
compile time 0 has been transformed to a run time argument of type
''int'', which cannot be casted to ''int*''.

Summing up, the compiler seems to be correct for the case of literal 0,
and for the case of NULL it might be that with this compiler NULL is
defined as 0L (for example), which it''s allowed to be, and then the two
arguments are not of the same type, and the template constructor is not
selected, which if that''s the case would mean the compiler is correct.

And which, if my analysis is correct, means that this almost trivial
little piece of code has Undefined Behavior (different compilers can
then freely elect to accept or reject the NULL case, depending on the
definition of NULL).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?




Alf P. Steinbach写道:

Alf P. Steinbach wrote:
* Chris Dams:
* Chris Dams:

我发现程序

#include< vector>
使用命名空间std;

int main()
{vector< int *> v(2,0);
返回0;


不使用g ++ 4.0.2编译。它说(包括相当多的STL错误消息):错误:错误的无效转换:从
const int到int *的无效转换。这让我尝试用v(2,NULL)代替v(2,0),现在它编译了。

然而,我似乎依稀记得NULL应该是#defined到在C ++中为0
,为什么会出现这个错误?编译器是否错误地抱怨这个?
你要反对的是使用两个迭代器选择模板化构造函数,而不是具有大小和默认值的构造函数
value(我_think_那是'因为默认值是通过引用传递给const,但有人纠正我,如果不是这样的话。)

I found out that the program

#include<vector>

using namespace std;

int main()
{ vector<int*> v(2,0);
return 0;
}

does not compile using g++ 4.0.2. It says (including quite a of STL-error
message): "error: invalid conversion from error: invalid conversion from
const int to int*". This made me try to replace v(2,0) by v(2,NULL) and now
it compiles.

However, I seem to vaguely remember that NULL should be #defined to be 0
in C++, so why this error? Is the a compiler wrong to complain about this?
What you''re up against is the selection of the templated constructor
with two iterators, instead of the constructor with a size and a default
value (I _think_ that''s because the default value is passed by reference
to const, but someone correct me if that''s not the case).




不,迭代器版本只是普通

重载规则的更好匹配。

NULL不是T *类型的对象,即使它可以是转换为

一。

另一方面,迭代器版本没有转换但需要

只有模板实例化。


....总结一下,对于文字0,<​​br />的情况,编译器似乎是正确的,对于NULL的情况,可能是这个编译器的NULL是
定义为0L(例如),它是允许的,然后两个
参数的类型不同, d,模板构造函数没有被选中,如果是这样的话就意味着编译器是正确的。

如果我的分析是正确的,那意味着这几乎是微不足道的<一小段代码有未定义的行为(不同的编译器可以随后自由选择接受或拒绝NULL的情况,具体取决于NULL的定义。)



No, the iterator version is simply a better match by the normal
overload rules.
NULL is not an object of type T*, even though it can be converted to
one.
On the other hand, the iterator version has no conversions but requires
only a template instantiation.

.... Summing up, the compiler seems to be correct for the case of literal 0,
and for the case of NULL it might be that with this compiler NULL is
defined as 0L (for example), which it''s allowed to be, and then the two
arguments are not of the same type, and the template constructor is not
selected, which if that''s the case would mean the compiler is correct.

And which, if my analysis is correct, means that this almost trivial
little piece of code has Undefined Behavior (different compilers can
then freely elect to accept or reject the NULL case, depending on the
definition of NULL).




这将是未指明的行为。 Undefined包含很多不好的
东西。

未指定意味着他们只能接受或拒绝它,而不是

记录

如何做出选择。但是,如果他们接受了,行为就是

仍然

受标准要求的约束。


HTH,

Michiel Salters



That would be unspecified behavior. Undefined includes lots of bad
things.
Unspecified means they can only accept or reject it, without
documenting
how the choice is made. However, if they accept it, the behavior is
still
bound by the requirements of the standard.

HTH,
Michiel Salters




Alf P. Steinbach写道:

Alf P. Steinbach wrote:
如果我的分析是正确的,那意味着这个几乎无足轻重的小代码片段有未定义的行为(不同的编译器可以随后自由选择接受或拒绝NULL情况,具体取决于
NULL的定义。
And which, if my analysis is correct, means that this almost trivial
little piece of code has Undefined Behavior (different compilers can
then freely elect to accept or reject the NULL case, depending on the
definition of NULL).




VC ++ 2005拒绝它但是第二个参数接受0L。



VC++ 2005 rejects it but accepts 0L for the second argument.


这篇关于无法在向量中为指针写入0。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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