为什么这段代码不能编译? [英] Why doesn't this code compile?

查看:101
本文介绍了为什么这段代码不能编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模板< class T>

void foo(std :: vector< T>& v)

{

std: :vector< T> :: iterator i = v.begin();

}


此代码在Visual C ++ 6下编译,但不在
Linux下的
g ++。有谁知道为什么?


消息是:


错误:预期`;''之前?我?

-

< \ ___ />

/ OO \

\ _____ / FTB。对于电子邮件,请删除我的袜子。


在科学中,科学家经常会说,''你知道

'这是一个非常好的论点;我的立场是错的,''

然后他们实际上改变了主意,你再也没有

再次听到他们的旧观点。他们真的这样做了。

它不会经常发生,因为科学家们需要人类,而且变化有时是痛苦的。但它每天都会发生
。我不记得最后一次发生在政治或宗教上的事情。


- Carl Sagan,1987年CSICOP主题演讲

解决方案

* fungus:


template< class T>

void foo(std :: vector< T>& v)

{

std :: vector< T> :: iterator i = v.begin();

}


此代码在Visual C ++ 6下编译,但在Linux下没有使用

g ++编译。有谁知道为什么?


消息是:


错误:预期`;''之前?我?



为什么不添加''typename''。请查看常见问题解答。如果这不是
已经是常见问题解答,请发送邮件给Marshall Cline。


谢谢。


-

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

问:为什么这么糟糕?

A:热门发布。

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


fungus写道:


template< class T>

void foo(std :: vector< T>& v)

{

std :: vector< T> :: iterator i = v.begin();

}


此代码在Visual C ++ 6下编译,但在Linux下没有使用

g ++编译。有谁知道为什么?


消息是:


错误:预期`;''之前?我?


只是为了放大Alf的评论,我的理解是这不合法

因为std :: vector< T> :: iterator是一个依赖类型(即,一种类型,其实际值取决于类型参数。我认为这行应该是:


typename std :: vector< T> :: iterator i = v.begin();


或者(更好,因为它使用RAII)


typename std :: vector< T> :: iterator i(v.begin());


PS不要依赖编译器来告诉你某些构造是否是合法的C ++ - 他们中的许多人还没有赶上标准。


干杯

吉姆。


James Bannon写道:


>

只是为了放大Alf的评论,我的理解是这不合法

因为std :: vector< T> :: iterator是一个依赖类型(即,

实际值取决于类型参数的类型)。



稍微进一步放大:它是一个依赖名称,而不是

依赖类型。名称的含义(包括它是否命名为

类型或其他类型)取决于向量< T>的定义,其中,

如你所暗,取决于T的实际类型,因此不能仅仅根据模板定义来确定



template <class T>
void foo(std::vector<T>& v)
{
std::vector<T>::iterator i = v.begin();
}

This code compiles under Visual C++ 6 but not with
g++ under Linux. Anybody know why?

The message is:

error: expected `;'' before ?i?

--
<\___/>
/ O O \
\_____/ FTB. For email, remove my socks.

In science it often happens that scientists say, ''You know
that''s a really good argument; my position is mistaken,''
and then they actually change their minds and you never
hear that old view from them again. They really do it.
It doesn''t happen as often as it should, because scientists
are human and change is sometimes painful. But it happens
every day. I cannot recall the last time something like
that happened in politics or religion.

- Carl Sagan, 1987 CSICOP keynote address

解决方案

* fungus:

template <class T>
void foo(std::vector<T>& v)
{
std::vector<T>::iterator i = v.begin();
}

This code compiles under Visual C++ 6 but not with
g++ under Linux. Anybody know why?

The message is:

error: expected `;'' before ?i?

Why don''t you add ''typename''. Please check the FAQ. If this isn''t
already a FAQ, please send a mail to Marshall Cline about it.

Thanks.

--
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?


fungus wrote:

template <class T>
void foo(std::vector<T>& v)
{
std::vector<T>::iterator i = v.begin();
}

This code compiles under Visual C++ 6 but not with
g++ under Linux. Anybody know why?

The message is:

error: expected `;'' before ?i?

Just to amplify Alf''s remark, my understanding is that this is not legal
since std::vector<T>::iterator is a dependent type (i.e., a type whose
actual "value" depends on a type parameter). I think the line should read:

typename std::vector<T>::iterator i = v.begin ();

or (better because it uses RAII)

typename std::vector<T>::iterator i(v.begin ());

P.S. Don''t rely on a compiler to tell you whether or not some construct
is legal C++ - many of them haven''t caught up with the Standard yet.

Cheers
Jim.


James Bannon wrote:

>
Just to amplify Alf''s remark, my understanding is that this is not legal
since std::vector<T>::iterator is a dependent type (i.e., a type whose
actual "value" depends on a type parameter).

Just a bit of further amplification: it''s a dependent name, not a
dependent type. The meaning of the name (including whether it names a
type or something else) depends on the definition of vector<T>, which,
as you suggest, depends on the actual type of T, so can''t be determined
from looking only at the template definition.


这篇关于为什么这段代码不能编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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