g++ 中的奇怪错误
我的程序有一个类,其中包含名为 levels
的 Level
对象向量。在成员函数中有这样一行:
levels.push_back(level::Level());
我今天对程序做了一些更改,并且该行代码开始出现段错误:
0x0804d248 in void std::vector<yarl::level::Level, std::allocator<yarl::level::Level> >::emplace_back<yarl::level::Level>(yarl::level::Level&&) (this=0x0,
__args#0=...) at /usr/include/c++/4.4/bits/vector.tcc:93
93 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
我认为 Level
对象可能已以某种方式损坏,所以我在外部声明了它函数调用,以便我可以在 gdb 中检查它,如下所示:
level::Level foo();
levels.push_back(foo);
结果这无法编译。它 g++ 给出了两个我以前从未见过的错误:
error: invalid conversion from 'level::Level (*)()' to 'int'
error: initializing argument 1 of 'level::Level::Level(int, int, int)'
现在,Level
的构造函数采用三个整数参数,每个参数都有默认值。我认为它可能会抱怨我没有传递这三个参数,即使它们有默认值,所以我更改了第一行以传递默认值:
level::Level foo(1, 100, 100);
现在可以编译,并且仍然存在段错误,但是在不同的地方(尽管是相同的测试):
0x0804c699 in std::vector<yarl::level::Level, std::allocator<yarl::level::Level> >::push_back (this=0x0, __x=...) at /usr/include/c++/4.4/bits/stl_vector.h:735
735 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
我意识到这代码太少了,无法期望你们能够解决我的问题,但也许有人可以告诉我更多关于这些错误的含义?特别是那些 g++ 错误;我不知道为什么它不接受空的 Level
构造函数,因为它的所有参数都是默认的,而且我不知道 (*)()
部分是什么错误的含义(这使得该错误对谷歌来说非常令人沮丧)。
My program has a class with a vector of Level
objects named levels
. In a member function there is this line:
levels.push_back(level::Level());
I made several changes to my program today, and that line of code started segfaulting:
0x0804d248 in void std::vector<yarl::level::Level, std::allocator<yarl::level::Level> >::emplace_back<yarl::level::Level>(yarl::level::Level&&) (this=0x0,
__args#0=...) at /usr/include/c++/4.4/bits/vector.tcc:93
93 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
I thought that the Level
object may have somehow become corrupt, so I declared it outside the function call so I could inspect it in gdb, like this:
level::Level foo();
levels.push_back(foo);
Turns out this doesn't compile. It g++ gives two errors I haven't seen before:
error: invalid conversion from 'level::Level (*)()' to 'int'
error: initializing argument 1 of 'level::Level::Level(int, int, int)'
Now, Level
's constructor takes three integer parameters, each with default values. I thought it might have been complaining that I hadn't passed those three parameters, even though they have defaults, so I changed the first line to pass the value of the defaults:
level::Level foo(1, 100, 100);
This now compiles, and still segfaults, but does so at a different spot (although an identical test):
0x0804c699 in std::vector<yarl::level::Level, std::allocator<yarl::level::Level> >::push_back (this=0x0, __x=...) at /usr/include/c++/4.4/bits/stl_vector.h:735
735 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
I realize this is too little code to expect you guys to be able to solve my problem, but perhaps someone could tell me a little more about what these errors mean? Those g++ errors especially; I don't know why it wouldn't accept an empty Level
constructor given that all its parameters are default, and I have no idea what the (*)()
part of the error means (it makes the error a very frustrating one to google).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
至少其中一个错误非常简单。这
是 C++ 中相当常见的错误,称为最令人烦恼的解析。您不是声明一个名为
foo
且类型为level::Level
的变量,而是向前声明一个具有该名称和返回类型且不带参数的函数。删除括号以声明变量您的其他调用失败,因为(如堆栈跟踪中所示)
this
为 NULL,但对我来说,原因并不明显,特别是当您似乎正在操作时堆栈变量而不是指针At least one of the errors is pretty simple. This:
is a fairly common mistake in C++ known as the most vexing parse. You're not declaring a variable with the name
foo
and typelevel::Level
, you're forward declaring a function with that name and return type that takes no arguments. Remove the parentheses to declare a variableYour other calls are failing because (as indicated in the stack trace)
this
is NULL, but it's not obvious to me why that is, particularly as you appear to be operating on a stack variable and not a pointer它声明了一个名为
foo
的函数,该函数不带参数并返回level::Level
。这称为 最令人烦恼的解析。这使用默认构造函数创建了一个level::Level
:至于您原来的问题,您似乎有一个 null
this
指针:如果
levels< ,则可能会发生这种情况/code> 是对 null 的引用。您是否通过取消引用空指针来创建引用?
That declares a function named
foo
that takes no arguments and returns alevel::Level
. This is known as the most vexing parse. This creates alevel::Level
using the default constructor:As for your original problem, you appear to have a null
this
pointer:This could happen if
levels
is a reference to null. Did you create the reference by dereferencing a null pointer?我不知道错误的 (*)() 部分意味着什么(这使得该错误对谷歌来说非常令人沮丧)。
这样的代码:
不会创建名为 obj 且默认的对象实例初始化它。它声明一个不带参数的函数,返回一个 Object 实例。显然你的向量没有被模板化来保存指向此类函数的指针。
您的其余问题是由于 42 号线缺少毛巾操作员造成的。
I have no idea what the (*)() part of the error means (it makes the error a very frustrating one to google).
Code like this:
does not create an Object instance called obj and default initialize it. It declares a function with no parameters that returns an Object instance. Apparently your vector is not templated to hold pointers to such functions.
The rest of your issues are caused be the lack of towel operator on line 42.
您需要阅读 STL 集合对其模板化参数的期望。他们期望项目类型模板参数至少具有“值”行为。 int 具有值行为,因为如果您有
那么
std::vector 类将使用具有重载赋值运算符和复制构造函数的对象。 std::map 和其他一些集合需要其他东西,例如比较运算符重载。
这是一个带有复制构造函数、默认构造函数和赋值运算符重载的类:
You need to read up on on the STL collection's expectations for their templated arguments. They expect at a minimum the item type template arguments to have "value" behavior. An int has value behavior because if you have
Then
the std::vector class will work with objects that have an overloaded assignment operator and a copy constructor. std::map and some other collections need other things like comparison operator overloads.
Here's a class with a copy constructor, a default constructor, and an assignment operator overload: