请求从'...'转换为非标量类型'...' [英] conversion from '...' to non-scalar type '...' requested
问题描述
你好@all,
我的小例子不编译......我知道,这个(一如既往)有
的原因,但我想知道为什么?
顺便说一句:
我只能得到g ++(4.x),BCB(6.0),......的错误。 >
VS C ++(2005)完美运行(没有警告等)
class classValue
{
public :
classValue(){}
};
class classHolder
{
public:classHolder(classValue& ami){} //< ==我知道,但在
这个案例引用必须是非const
};
classHolder getAHolder()
{
return classValue(); //< ---错误:从
''classValue''转换为非标量类型''classHolder''请求
}
使用一个小技巧,外翻效果很好......
class classValue
{
public:
classValue(){}
classValue& self(){return * this; }
};
class classHolder
{
public:classHolder(classValue& ami ){}
};
classHolder getAHolder()
{return classValue()。self();}
但为什么我必须使用这个讨厌的间接?
如何调用这种现象?
还有其他建议吗?
THX,
Kirsten
Hi @all,
My small example does not compile... I know, that this (as always) has
reasons, but I want to know WHY?
BTW:
I only get errors with g++ (4.x), BCB (6.0),...
VS C++ (2005) works perfectly (without warnings etc.)
class classValue
{
public:
classValue() {}
};
class classHolder
{
public: classHolder(classValue &ami) {} // <== I know, but in
this case reference MUST be non-const
};
classHolder getAHolder()
{
return classValue(); // <--- Error: conversion from
''classValue'' to non-scalar type ''classHolder'' requested
}
Using a little trick, everthing works well...
class classValue
{
public:
classValue() {}
classValue &self() { return *this; }
};
class classHolder
{
public: classHolder(classValue &ami) {}
};
classHolder getAHolder()
{ return classValue().self();}
But why must I use this nasty indirection?
How is this phenomenon called?
Any other suggestions?
THX,
Kirsten
推荐答案
tt ****** @ gmx.de 写道:
我的小例子不编译......我知道,这(一如既往)有原因,但我想知道为什么?
因为它被标准禁止。
BTW:
我只得到g ++(4.x),BCB( 6.0),...
VS C ++(2005)完美运行(没有警告等)
确保禁用语言扩展用VC ++编译时。
class classValue
{
public:
classValue(){}
};
类classHolder
公共:classHolder(classValue& ami){} //< ==我知道,但在
这个案例中引用必须是非const
};
classHolder getAHolder()
{
返回classValue(); //< ---错误:从
''classValue''转换为非标量类型''classHolder''请求
这是正确的。临时不能绑定到非const引用。
}
使用一个小技巧,everthing效果很好......
class classValue
{
公开:
classValue(){}
classValue& self(){return * this; } class.golder
{
public:classHolder(classValue& ami){}
};
> classHolder getAHolder()
{return classValue()。self();}
但为什么我必须使用这个讨厌的间接?
实际使用它是一个非常糟糕的主意。
如何调用这种现象?
哪一个?玩脏伎俩?
还有其他建议吗?
My small example does not compile... I know, that this (as always) has
reasons, but I want to know WHY?
Because it''s prohibited by the Standard.
BTW:
I only get errors with g++ (4.x), BCB (6.0),...
VS C++ (2005) works perfectly (without warnings etc.)
Make sure you disable "language extensions" when compiling with VC++.
class classValue
{
public:
classValue() {}
};
class classHolder
{
public: classHolder(classValue &ami) {} // <== I know, but in
this case reference MUST be non-const
};
classHolder getAHolder()
{
return classValue(); // <--- Error: conversion from
''classValue'' to non-scalar type ''classHolder'' requested
That''s correct. A temporary cannot be bound to a non-const reference.
}
Using a little trick, everthing works well...
class classValue
{
public:
classValue() {}
classValue &self() { return *this; }
};
class classHolder
{
public: classHolder(classValue &ami) {}
};
classHolder getAHolder()
{ return classValue().self();}
But why must I use this nasty indirection?
It''s a very bad idea to actually use it.
How is this phenomenon called?
Which one? Playing dirty tricks?
Any other suggestions?
重新设计。
V >
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要不要问
Redesign.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
tthun ... @ gmx.de写道:
tthun...@gmx.de wrote:
嗨@all,
我的小例子不编译......我知道,这个(一如既往)有原因,但我想知道为什么?
BTW:
我只会得到错误g ++(4.x),BCB(6.0),......
VS C ++(2005)完美运行(没有警告等)
VS *构建*完美。我怀疑它实际上会在实践中起作用
除了纯粹的机会。
class classValue
{
public:
classValue(){ } class.golder
公共:classHolder(classValue& ami){} //< ==我知道,但是在
};
classHolder getAHolder()
{
返回classValue(); //< ---错误:从
''classValue''转换为非标量类型''classHolder''请求
}
你是返回类型classHolder,所以你指定的返回值(类型为classValue的
* temporary *)必须通过
转换为classHolder你提供的构造函数。但是,根据C ++标准,
非const引用不能绑定到temporaries,这是一个很好的
的东西,为了你的保护。
使用一个小技巧,everthing效果很好......
class classValue
{
public:
classValue(){}
classValue& self(){return * this; } class.golder
{
public:classHolder(classValue& ami){}
};
> classHolder getAHolder()
{return classValue()。self();}
但为什么我必须使用这种令人讨厌的间接?
这种现象如何被称为?
还有其他任何建议吗?
Hi @all,
My small example does not compile... I know, that this (as always) has
reasons, but I want to know WHY?
BTW:
I only get errors with g++ (4.x), BCB (6.0),...
VS C++ (2005) works perfectly (without warnings etc.)
VS *builds* perfectly. I doubt it would actually work in practice
except by pure chance.
class classValue
{
public:
classValue() {}
};
class classHolder
{
public: classHolder(classValue &ami) {} // <== I know, but in
this case reference MUST be non-const
};
classHolder getAHolder()
{
return classValue(); // <--- Error: conversion from
''classValue'' to non-scalar type ''classHolder'' requested
}
You are returning type classHolder, so the return value you specify (a
*temporary* of type classValue) must be converted to a classHolder via
the constructor you supply. However, according to the C++ Standard,
non-const references cannot bind to temporaries, and that is a good
thing and for your protection.
Using a little trick, everthing works well...
class classValue
{
public:
classValue() {}
classValue &self() { return *this; }
};
class classHolder
{
public: classHolder(classValue &ami) {}
};
classHolder getAHolder()
{ return classValue().self();}
But why must I use this nasty indirection?
How is this phenomenon called?
Any other suggestions?
你在这里违反了语言的规则,这个
欺骗实际上不会让你任何地方,因为你可能会在你的真实代码中悬挂一个悬挂参考结果。
。不要暂时使用
或者不要将非const引用传递给
classHolder :: classHolder() - 也就是说,要么
classValue对象的副本或传递const引用。
干杯! --M
You are fighting against the rules of the language here, and this
trickery won''t actually get you anywhere since you''ll likely end up
with a dangling reference in your real code. Either don''t use a
temporary or don''t pass a non-const reference to
classHolder::classHolder() -- that is, either make a copy of the
classValue object or pass a const reference to it.
Cheers! --M
tt ****** @ gmx.de 写道:
嗨@all,
我的小例子不编译......我知道,这个(一如既往)有
因为你不能将rvalue绑定到非const引用。
BTW:
我只得到g ++(4.x)的错误,BCB(6.0),......
VS C ++(2005)完美运作(没有警告等)
class classValue
{
公开:> classValue(){}
};
class classHolder
{
public:classHolder(classValue& ami){} //< ==我知道,但在
这种情况下引用必须是非const
};
classHolder getAHolder()
{
返回classValue(); //< ---错误:从
''classValue''转换为非标量类型''classHolder''请求
如果你能做到这一点,你还有几个更重要的问题:
void f(int& i)
{
i = 2;
}
int main()
{
int i = 4;
double d = 1.0;
f(i); // ok
f(d); //好吧?
f(4); //哎哟!
//这个输出应该是什么?
std :: cout<< d;
}
对于f()的第二次调用,double被转换为临时的
int(一个rvalue),在f()中被修改。你是否期望''d''还能修改?b $ b?它不会那么具有误导性,危险性和非法性。
}
使用一个小技巧,everthing效果很好......
是的,你可以这样做。
class classValue
{
public:
classValue(){}
classValue& self(){return * this; } class.golder
{
public:classHolder(classValue& ami){}
};
> classHolder getAHolder()
{return classValue()。self();}
但为什么我必须使用这个讨厌的间接?
Hi @all,
My small example does not compile... I know, that this (as always) has
reasons, but I want to know WHY?
Because you cannot bound an rvalue to a non-const reference.
BTW:
I only get errors with g++ (4.x), BCB (6.0),...
VS C++ (2005) works perfectly (without warnings etc.)
class classValue
{
public:
classValue() {}
};
class classHolder
{
public: classHolder(classValue &ami) {} // <== I know, but in
this case reference MUST be non-const
};
classHolder getAHolder()
{
return classValue(); // <--- Error: conversion from
''classValue'' to non-scalar type ''classHolder'' requested
If you could do that, you''d get several more important problems:
void f(int& i)
{
i = 2;
}
int main()
{
int i = 4;
double d = 1.0;
f(i); // ok
f(d); // ok??
f(4); // ouch!
// what should this output?
std::cout << d;
}
For the second call to f(), the double gets converted to a temporary
int (an rvalue), which gets modified in f(). Do you expect ''d'' to get
modified also? It won''t and that''s misleading, dangerous and illegal.
}
Using a little trick, everthing works well...
Yes, you may do that.
class classValue
{
public:
classValue() {}
classValue &self() { return *this; }
};
class classHolder
{
public: classHolder(classValue &ami) {}
};
classHolder getAHolder()
{ return classValue().self();}
But why must I use this nasty indirection?
因为这就是语言的定义方式。请注意,您也可以使用
classholder getAHolder()
{
classValue v;
return classHolder(v);
}
Jonathan
Because that''s how the language is defined. Note that you may also do
classholder getAHolder()
{
classValue v;
return classHolder(v);
}
Jonathan
这篇关于请求从'...'转换为非标量类型'...'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!