Undefined reference to #39;operator delete(void*)#39;(对“操作员删除(void *)的未定义引用)
问题描述
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
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
and delete
. In the simplest case, you could simply wrap malloc
, or allocate memory from your own favourite source:
void * operator new(std::size_t n)
{
void * const p = std::malloc(n);
// handle p == 0
return p;
}
void operator delete(void * p) // or delete(void *, std::size_t)
{
std::free(p);
}
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.
这篇关于对“操作员删除(void *)"的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:对“操作员删除(void *)"的未定义引用
- C语言手把手带你掌握带头双向循环链表 2023-04-03
- C语言qsort()函数的使用方法详解 2023-04-26
- C++ 数据结构超详细讲解顺序表 2023-03-25
- 我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上? 2022-10-30
- C语言详解float类型在内存中的存储方式 2023-03-27
- ubuntu下C/C++获取剩余内存 2023-09-18
- 详解C语言中sizeof如何在自定义函数中正常工作 2023-04-09
- c++ const 成员函数,返回一个 const 指针.但是返回的指针是什么类型的 const? 2022-10-11
- Easyx实现扫雷游戏 2023-02-06
- Qt计时器使用方法详解 2023-05-30