g++ 中的奇怪错误

发布于 2024-09-06 19:23:04 字数 1523 浏览 5 评论 0原文

我的程序有一个类,其中包含名为 levelsLevel 对象向量。在成员函数中有这样一行:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒浓于脸红 2024-09-13 19:23:04

至少其中一个错误非常简单。这

level::Level foo();

是 C++ 中相当常见的错误,称为最令人烦恼的解析。您不是声明一个名为 foo 且类型为 level::Level 的变量,而是向前声明一个具有该名称和返回类型且不带参数的函数。删除括号以声明变量

您的其他调用失败,因为(如堆栈跟踪中所示)this 为 NULL,但对我来说,原因并不明显,特别是当您似乎正在操作时堆栈变量而不是指针

At least one of the errors is pretty simple. This:

level::Level foo();

is a fairly common mistake in C++ known as the most vexing parse. You're not declaring a variable with the name foo and type level::Level, you're forward declaring a function with that name and return type that takes no arguments. Remove the parentheses to declare a variable

Your 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

笑着哭最痛 2024-09-13 19:23:04
level::Level foo();

它声明了一个名为 foo 的函数,该函数不带参数并返回 level::Level。这称为 最令人烦恼的解析。这使用默认构造函数创建了一个 level::Level

level::Level foo;

至于您原来的问题,您似乎有一个 null this 指针:

... ::push_back (this=0x0, __x=...) ...

如果 levels< ,则可能会发生这种情况/code> 是对 null 的引用。您是否通过取消引用空指针来创建引用?

level::Level foo();

That declares a function named foo that takes no arguments and returns a level::Level. This is known as the most vexing parse. This creates a level::Level using the default constructor:

level::Level foo;

As for your original problem, you appear to have a null this pointer:

... ::push_back (this=0x0, __x=...) ...

This could happen if levels is a reference to null. Did you create the reference by dereferencing a null pointer?

冷默言语 2024-09-13 19:23:04

我不知道错误的 (*)() 部分意味着什么(这使得该错误对谷歌来说非常令人沮丧)。

这样的代码:

Object obj();

不会创建名为 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:

Object obj();

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.

懵少女 2024-09-13 19:23:04

您需要阅读 STL 集合对其模板化参数的期望。他们期望项目类型模板参数至少具有“值”行为。 int 具有值行为,因为如果您有

int a; // default constructor works

那么

a = 3; // assignment works
int c(a); // copy construction works

std::vector 类将使用具有重载赋值运算符和复制构造函数的对象。 std::map 和其他一些集合需要其他东西,例如比较运算符重载。

这是一个带有复制构造函数、默认构造函数和赋值运算符重载的类:

class foo
{
private:
  int m_internalVal;
public:
  foo() : m_internalVal(0) { }
  foo(const foo& other) : m_internalVal(other.m_internalVal) { }
  foo& operator =(const foo& rval) { m_internalVal = rval.m_internalVal; return *this; }
};

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

int a; // default constructor works

Then

a = 3; // assignment works
int c(a); // copy construction works

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:

class foo
{
private:
  int m_internalVal;
public:
  foo() : m_internalVal(0) { }
  foo(const foo& other) : m_internalVal(other.m_internalVal) { }
  foo& operator =(const foo& rval) { m_internalVal = rval.m_internalVal; return *this; }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文