已解决:
感谢多米尼克·哈蒙,我明白了。
这一切都归结为尝试在一个有点空对象上调用函数。它可以使用对象的一部分,但不能使用其他部分。
我不知道这会发生。
问题
我遇到了一个奇怪的分段错误,
std::map< IntVector3, std::vector<IntVector3> >.
在我的 MyClass.hpp 文件中,我将其设为私有属性:
std::map< IntVector3, std::vector< IntVector3 > > recurData;
在 MyClass 的构造函数中的 MyClass.cpp 文件中,我可以运行
std::vector< IntVector3 > pt;
pt.push_back(IntVector3(1,2,3));
recurData[IntVector3(1,2,3)] = pt;
它运行正确,并且没有出现错误。
稍后在程序中,我调用 MyClass 中的一个函数来执行相同的操作,即更改 recurData。不在构造函数中。这会导致分段错误。据我所知,没有其他变化会影响 recurData。
IntVector3的实现是:
http://pastebin.com/Hc83xapk
我添加了很多不需要的额外运算符(> =,<=...)。
(我试图遵循 3 的规则)
唯一真正奇怪的是 <<操作员。这使用 std::string 进行比较。如果 x、y、z 低于 99,这个快速技巧应该适用于所有 x、y、z。
感谢您的帮助,这让我发疯。
SOLVED:
Thanks figured it out thanks to dominic hamon.
It all boils down to trying to call a function on a kinda null object. It could use parts of the object but not others.
I had no idea that this could even happen.
Question
I have experienced an odd segmentation fault with a
std::map< IntVector3, std::vector<IntVector3> >.
In my MyClass.hpp file I make this a private property:
std::map< IntVector3, std::vector< IntVector3 > > recurData;
In my MyClass.cpp file in the constructor for MyClass i can run
std::vector< IntVector3 > pt;
pt.push_back(IntVector3(1,2,3));
recurData[IntVector3(1,2,3)] = pt;
This runs correctly and i don't get a fault.
Later in the program i call a function in MyClass that does the same thing, altering of recurData. Not in the constructor. This causes a segmentation fault. There is no other change to my knowledge that affects recurData.
The implementable of IntVector3 is:
http://pastebin.com/Hc83xapk
There is a lot of extra operators that i added that are unneeded (>=,<=...).
(I tried to follow the rule of 3)
The only real oddness is the < operator. This uses a std::string to compare. This quick hack should work for all x,y,z if they are under 99.
Thanks for the help, this has been driving me crazy.
发布评论
评论(1)
使用字符串来实现比较函数(a)效率极低,并且(b)损坏。它不会为您提供严格弱排序,而这是
std::map
所需要的。特别是,它不会是传递性的,即如果a
a
a
a
a
a
a
a
a
a b
和b < c
,它不一定会给你a
c
.这将会彻底搞乱 std::map。<
的典型实现类似于:Using a string to achieve a comparison function is (a) massively inefficient, and (b) broken. It will not provide you a strict-weak ordering, which is what is required for
std::map
. In particular, it is not going to be transitive, i.e. ifa < b
andb < c
, it won't necessarily give you thata < c
. This is going to totally mess upstd::map
.A typical implementation of
<
would be something like: