简体   繁体   English

c ++ list,没有'value_type'的可行转换

[英]c++ list, no viable conversion from 'value_type'

I'm using list in c++ 我在c ++中使用list

std::list<Foo> foo;
foo.push_back(bar);
Foo *ret = foo.back();

When compile I obtain this error (at last line): 编译时我得到这个错误(在最后一行):

No viable conversion from 'value_type' (aka Foo) to 'Foo *'

How can I solve this? 我怎么解决这个问题?

Thanks 谢谢

foo.back() will return a reference (ie Foo & ), but you're trying to assign it to a pointer. foo.back()将返回一个引用(即Foo & ),但是你试图将它分配给一个指针。

To get it to work, you can assign to a reference instead: 要使其工作,您可以改为分配参考:

Foo &ret = foo.back();

Or get the address of the item returned, and assign that to a pointer: 或者获取返回项的地址,并将其分配给指针:

Foo *ret = &foo.back();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++错误:类型返回的值没有可行的转换 - C++ Error: no viable conversion from returned value of type C ++设置方法:函数&#39;setCost&#39;不可行:&#39;this&#39;参数的类型为&#39;const value_type&#39; - C++ set method: function 'setCost' not viable: 'this' argument has type 'const value_type' 没有从“value_type”(又名“char”)到“string”(又名“basic_string”)的可行转换<char, char_traits<char> , 分配器<char> &gt;&#39;) - no viable conversion from 'value_type' (aka 'char') to 'string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') C ++错误:没有从&#39;mapped_type&#39;到&#39;int&#39;的可行转换 - C++ error: no viable conversion from 'mapped_type' to 'int' 具有自定义value_type的C ++映射 - C++ map with custom value_type 没有从“int”到“student”的可行转换 C++ - no viable conversion from 'int' to 'student' c++ 没有可行的从“Class *”到“Class”C ++的转换 - No viable conversion from 'Class *' to 'Class' C++ C ++语义问题,指向指针的const value_type * - C++ semantic issue, const value_type to pointer* 从引用容器 C++ 获取 value_type 的优雅方式 - Elegant way to get value_type from referenced container C++ C ++的value_type是否可以从iterator_traits扩展为所有类型? - Can C++'s value_type be extended from iterator_traits to all types?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM