【发布时间】:2016-07-15 05:07:17
【问题描述】:
我正在创建一个指向新对象的新指针,并立即将 push_front 放入双端队列。我想改用 emplace_front 但出现编译器错误。
我的对象构造函数需要 1 个字符串参数。
std::deque<NetPacket*> q_IncomingPackets;
q_IncomingPackets.push_back(new NetPacket(std::string(uncompressed_data, retVal))));
q_IncomingPackets.emplace_back(std::string(uncompressed_data, retVal));
根据其他网站的参考资料,我想我可以简单地将push_back 换成emplace_back,但得到以下错误:
Error C2440 'initializing': cannot convert from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'PeerNet::NetPacket *' PeerNet \vc\include\xmemory0 737
在创建指向对象的新指针时,引用并没有谈论使用 emplace,而只是具体的对象。 cppreference.comcplusplus.com
【问题讨论】:
标签: c++ object initialization deque