对“operator delete(void*)”的未定义引用
我是 C++ 编程新手,但已经在 C 和 Java 领域工作了很长时间。我正在尝试在我正在处理的某些串行协议中执行类似接口的层次结构,并不断收到错误:(
Undefined reference to 'operator delete(void*)'
简化的)代码如下:
PacketWriter.h:
class PacketWriter {
public:
virtual ~PacketWriter() {}
virtual uint8_t nextByte() = 0;
}
StringWriter.h:
class StringWriter : public PacketWriter {
public:
StringWriter(const char* message);
virtual uint8_t nextByte();
}
构造函数和 nextByte 函数已实现在 StringWriter.cpp 中,但没有其他内容。我需要能够从指向 PacketWriter 的指针中删除 StringWriter,并且如果我为 StringWriter 定义析构函数(无论是否是虚拟的),我就会收到各种其他类似的错误。我确信这是一个我作为新手忽略的简单问题。
另外,我正在为 AVR 芯片编写此内容,在 Windows 上使用 avr-g++。
谢谢
I'm new to C++ programming, but have been working in C and Java for a long time. I'm trying to do an interface-like hierarchy in some serial protocol I'm working on, and keep getting the error:
Undefined reference to 'operator delete(void*)'
The (simplified) code follows below:
PacketWriter.h:
class PacketWriter {
public:
virtual ~PacketWriter() {}
virtual uint8_t nextByte() = 0;
}
StringWriter.h:
class StringWriter : public PacketWriter {
public:
StringWriter(const char* message);
virtual uint8_t nextByte();
}
The constructor and nextByte functions are implemented in StringWriter.cpp, but nothing else. I need to be able to delete a StringWriter from a pointer to a PacketWriter, and i've been getting various other similar errors if I define a destructor for StringWriter, virtual or not. I'm sure it's a simple issue that I'm overlooking as a newbie.
Also, I'm writing this for an AVR chip, using avr-g++ on Windows.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
很抱歉在旧帖子中发帖,但它在 Google 搜索结果中的排名仍然很高,如果您遇到此问题,您确实应该查看 这个链接,因为它说你只需要链接 -lstdc++ 这就是为我解决问题的方法。
社区添加了以下行,但没有突出显示它,而是出于我无法理解的原因只是在我的答案中添加了评论:
“或者使用将隐式添加 -lstdc++ 选项的 C++ 编译器,例如 g++。”
Sorry for posting in an old thread, but it's still pretty high in Google's search results and if you have this problem, you really should check out this link, because there it says that you simply need to link -lstdc++ and this is what solved the problem for me.
The following line was added by the Community without highlighting it as such and instead of just adding a comment to my answer for reasons that elude me:
"Or use a C++ compiler that will implicitly add the -lstdc++ option, e.g. g++."
如果您由于某种原因没有链接到标准库(嵌入场景中很可能出现这种情况),则必须提供自己的运算符
new
和delete
。在最简单的情况下,您可以简单地包装malloc
,或者从您自己喜欢的源分配内存:如果您正在为普通的托管平台进行编译,那么您永远不必这样做,因此如果您确实需要为此,您最好熟悉平台上内存管理的复杂性。
If you are not linking against the standard library for some reason (as may well be the case in an embedded scenario), you have to provide your own operators
new
anddelete
. In the simplest case, you could simply wrapmalloc
, or allocate memory from your own favourite source:You should never have to do this if you are compiling for an ordinary, hosted platform, so if you do need to do this, you better be familiar with the intricacies of memory management on your platform.
我只会引用文档,因为他们说得更好。
I will just quote the documentation, since they put it better.
如果你只是想做一个界面,你不需要new/delete。只需从基类析构函数中删除“虚拟”,并确保派生类具有以下实现
__cxa_pure_virtual()。
这是一个可编译的示例。 (为了简单起见,我删除了返回,但它对它们来说工作得很好。)
在PacketWriter.h中
在StringWriter.h中
在StringWriter.cpp中
使用
avr-g++ StringWriter.cpp
进行编译If you're just looking to do an interface, you don't need new/delete. Just remove the "virtual" from the base class destructor, and make sure the derived class has an implementation of
__cxa_pure_virtual().
Here is a compilable example. (I removed the returns to keep things simple, but it works just fine with them.)
In PacketWriter.h
In StringWriter.h
In StringWriter.cpp
Compile with
avr-g++ StringWriter.cpp