为什么不编译......? [英] Why doesn't this compile....?

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

问题描述

我有一个基类foo:


class foo {

....

};

和派生类foo_d:


class foo_d:public foo {

};

然后我在构造函数中创建另一个类foo




class bar {

public:

bar(foo&);

};

所有简单的东西......但是当我尝试这个时编译器

抱怨:


bar b(foo_d());


OTOH这没关系:


foo_d f;

bar b(f);


为什么......?


-

< \ ___ />

/ OO \

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


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

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

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

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

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


- Carl Sagan,1987年CSICOP主题演讲

解决方案

fungus< um ***** @ SOCKSartlum.comwrote:


class foo {

...

};


class foo_d:public foo {

};


class bar {

public:

bar(foo&);

};


抱怨:


bar b(foo_d());


OTOH这没关系:


foo_d f;

bar b(f);


为什么...?



在第一种情况下,你创建一个临时的''foo_d''对象,并希望

将其传递给''的'ctor''酒吧''。问题是,临时工具只能绑定到_const_引用。但是''bar''ctor需要一个

非const引用。在第二种情况下你没有这个问题,

,因为没有临时的。


你可以通过更改''bar'的构造函数来解决这个问题'拿一个

''foo_d const&''。

hth

-

jb


(回复地址在rot13,先解读)


fungus写道:


>


所有简单的东西......但是当我尝试这个时编译器

抱怨:


bar b(foo_d());


OTOH这没关系:


foo_d f;

bar b(f);


为什么......?



你不能将rvalues绑定到非const引用。

使构造函数

bar(const foo&);


Jakob Bieling写道:


>


> ....抱怨:

bar b(foo_d());

OTOH这个没关系:

foo_d f;
bar b(f);

为什么......?



您可以通过更改''bar''的构造函数来修复此问题,以获取

''foo_d const&''。



我想到了......但是当我尝试它时,我得到一个

不同的错误:


test.cpp:错误:?foo :: foo(const foo&)?是私有的

在构造函数中?bar :: bar()?

警告:合成方法?foo_d :: foo_d(const foo_d&)?

首先需要这里

(是的,foo :: foo(const foo&)被宣布为私有...)

-

< \ ___ />

/ OO \

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


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

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

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

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

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


- Carl Sagan,1987年CSICOP主题演讲


I''ve got a base class foo:

class foo {
....
};
And a derived class foo_d:

class foo_d : public foo {
};
I then make another class which takes a foo
in the constructor:

class bar {
public:
bar(foo&);
};
All easy stuff... but when I try this the compiler
complains:

bar b(foo_d());

OTOH this is ok:

foo_d f;
bar b(f);

Why...?

--
<\___/>
/ 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 <um*****@SOCKSartlum.comwrote:

class foo {
...
};

class foo_d : public foo {
};

class bar {
public:
bar(foo&);
};

complains:

bar b(foo_d());

OTOH this is ok:

foo_d f;
bar b(f);

Why...?

In the first case, you create a temporary ''foo_d'' object and want to
pass this to the ctor of ''bar''. The problem is, that temporaries can
only be bound to _const_ references. But the ''bar'' ctor takes a
non-const reference. In the second case you do not have this problem,
because there is no temporary.

You can fix this by changing the constructor of ''bar'' to take a
''foo_d const&''.
hth
--
jb

(reply address in rot13, unscramble first)


fungus wrote:

>

All easy stuff... but when I try this the compiler
complains:

bar b(foo_d());

OTOH this is ok:

foo_d f;
bar b(f);

Why...?

You can''t bind rvalues to non-const references.
Make the constructor
bar(const foo&);


Jakob Bieling wrote:

>

>....complains:

bar b(foo_d());

OTOH this is ok:

foo_d f;
bar b(f);

Why...?


You can fix this by changing the constructor of ''bar'' to take a
''foo_d const&''.

I thought of that.... but when I try it I get a
different error:

test.cpp: error: ?foo::foo(const foo&)? is private
In constructor ?bar::bar()?
warning: synthesized method ?foo_d::foo_d(const foo_d&)?
first required here
(and yes, foo::foo(const foo&) is declared private...)
--
<\___/>
/ 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


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

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