未定义引用'operator delete(void *)' [英] Undefined reference to 'operator delete(void*)'

查看:1343
本文介绍了未定义引用'operator delete(void *)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++编程的新手,但是从事C和Java已有很长时间了.我正在尝试在我正在使用的某些串行协议中做类似接口的层次结构,并不断收到错误消息:

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*)'

(简化的)代码如下:

PacketWriter.h:

PacketWriter.h:

class PacketWriter {
public:
    virtual ~PacketWriter() {}
    virtual uint8_t nextByte() = 0;
}

StringWriter.h:

StringWriter.h:

class StringWriter : public PacketWriter {
public:
    StringWriter(const char* message);
    virtual uint8_t nextByte();
}

构造函数和nextByte函数在StringWriter.cpp中实现,但仅此而已.我需要能够从指向PacketWriter的指针中删除StringWriter,并且如果我为StringWriter定义了析构函数,无论是否为虚拟,我都会遇到各种其他类似的错误.我敢肯定,作为一个新手,这是一个简单的问题.

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.

此外,我正在Windows上使用avr-g ++为AVR芯片编写此代码.

Also, I'm writing this for an AVR chip, using avr-g++ on Windows.

谢谢

推荐答案

如果由于某种原因(例如,在嵌入式方案中可能不是)未与标准库链接,则必须提供自己的运算符newdelete.在最简单的情况下,您可以简单地包装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 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.

这篇关于未定义引用'operator delete(void *)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆