创建引用与创建对象之间的主要区别是什么?
不久前我正在玩 Boost.Extension 示例。 他们使用了
std::map<std::string, factory<computer> > computers;
computers.swap(types.get());
但是当我开始移植时项目 从 bjam 到 premake 到 Visual Studio Project 2008 我发现我不能使用他们用于创建地图的方法。我总是在该行看到 编译器错误 C2512 (实际上在Boost.Extension type_map.hpp内的第74行)。所以 我使用了创建地图链接的方法:(
map<string, factory<computer> >& computers(types.get());
他们在 一些教程)并且全部编译完毕。我对 C++ 很陌生,可能了解不多。
那么,在这种情况下和一般情况下,从链接创建地图与简单创建地图之间有什么区别?
更新 - 完整错误消息
错误 1 错误 C2512: boost::扩展::basic_type_map::type_map_convertible::type_holder: 没有合适的默认构造函数 可用 c:\users\avesta\downloads\extension-svn-source\boost\extension\type_map.hpp 74 多重继承
So a while ago I were playing with Boost.Extension example. They used
std::map<std::string, factory<computer> > computers;
computers.swap(types.get());
But when I started porting project from bjam to premake to visual studio project 2008 I found out that I can not use method they used for creating map. I always got Compiler Error C2512 on that line (actually on line 74 inside Boost.Extension type_map.hpp). So I used way of creating a link to a map:
map<string, factory<computer> >& computers(types.get());
(they used in some of there tutorials) and it all compiled. I am quite new to C++ and probably do not get alot.
So what is difference between creation of a map from link vs simple creation of a map, in this case and in general?
Update - full error message
Error 1 error C2512:
boost::extensions::basic_type_map::type_map_convertible::type_holder:
no appropriate default constructor
available c:\users\avesta\downloads\extension-svn-source\boost\extension\type_map.hpp 74 Mltiple-Inheritance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不同之处在于第二种情况根本不创建地图;它创建对已存在地图的引用。
types
包含一个映射,而types.get()
返回对该映射的引用,您可以用它来初始化您自己的引用。如果您使用该引用修改映射,那么您正在修改types
中包含的映射。第一种情况确实创建了一个空地图;然后它与
types
中包含的(可能非空)映射进行交换,然后将types
留空。这需要各阶层的更多支持;有些可能需要默认可构造、可交换,并且可能需要可复制和/或可分配。错误代码似乎表明其中一个类需要一个公共默认构造函数,但实际上没有;如果包含完整的错误消息,可能有助于诊断问题。The difference is that the second case doesn't create a map at all; it creates a reference to a map that already exists.
types
contains a map, andtypes.get()
returns a reference to that map, which you use to initialise your own reference. If you modify the map using that reference, then you are modifying the map contained intypes
.The first case does create an empty map; it then swaps it with the (presumably non-empty) map contained in
types
, leavingtypes
empty afterwards. This will require more support from the various classes involved; some might need to be default constructible, swappable, and possibly copyable and/or assignable. The error code seems to indicate that one of the classes needs a public default constructor, but doesn't have one; it might help to diagnose the problem if you include the whole error message.