首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Teensy 3中使用placement new

在Teensy 3中使用placement new
EN

Stack Overflow用户
提问于 2016-02-18 03:01:59
回答 1查看 477关注 0票数 2

我在Arduino/Teensy环境中有一个在".h“文件中定义的C++类。在".cpp“文件中,我尝试用一些代码做”新的放置“。我得到以下错误:

代码语言:javascript
复制
oscillator.h:17: error: no matching function for call to 'operator new(sizetype, AudioSynthWaveform*)'
_current_tone = static_cast<AudioStream*>(new (&_waveform) AudioSynthWaveform);
^
/tmp/build578ae2c22656d87e9d0d68db21416349.tmp/sketch/oscillator.h:17:68: note: candidate is:
In file included from /opt/arduino-1.6.7/hardware/teensy/avr/cores/teensy3/Printable.h:25:0,
from /opt/arduino-1.6.7/hardware/teensy/avr/cores/teensy3/Print.h:39,
from /opt/arduino-1.6.7/hardware/teensy/avr/cores/teensy3/Stream.h:24,
from /opt/arduino-1.6.7/hardware/teensy/avr/cores/teensy3/HardwareSerial.h:169,
from /opt/arduino-1.6.7/hardware/teensy/avr/cores/teensy3/WProgram.h:16,
from /opt/arduino-1.6.7/hardware/teensy/avr/cores/teensy3/Arduino.h:1,
from /tmp/build578ae2c22656d87e9d0d68db21416349.tmp/sketch/Synthesizer.ino.cpp:1:
/opt/arduino-1.6.7/hardware/teensy/avr/cores/teensy3/new.h:12:8: note: void* operator new(size_t)
void * operator new(size_t size);
^
/opt/arduino-1.6.7/hardware/teensy/avr/cores/teensy3/new.h:12:8: note:   candidate expects 1 argument, 2 provided
exit status 1
no matching function for call to 'operator new(sizetype, AudioSynthWaveform*)'

所以看起来问题是在很小的核心库中,位置new没有被定义-运算符只需要一个参数,而不是两个。

如果我像这样在".h“文件中定义自己的放置新实现,并将其包含在上面类的头文件中:

代码语言:javascript
复制
#ifndef NEW_H
#define NEW_H

void *operator new(size_t size, void *ptr){
  return ptr;
}

void operator delete(void *obj, void *alloc){
  return;
}

#endif //NEW_H

它似乎可以工作,但只有当我在头文件中的一个方法中使用placement new时。如果我将代码移出头文件并放入".cpp“实现文件中,我会得到一个类似的错误,即只需要一个参数。

有没有办法解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2016-02-18 11:46:18

我发现解决这个问题的最直接的方法就是

代码语言:javascript
复制
/opt/arduino-1.6.7/hardware/teensy/avr/cores/teensy3/new.h

并把原型放在

代码语言:javascript
复制
void *operator new(size_t size, void *ptr);

void operator delete(void *obj, void *alloc);

有多个重载运算符,然后是相关的".cpp“文件中的函数。

不知道为什么一开始就没有包括在内……

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35465405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档