使用新放置与 Teensy 3.
Using placement new with the Teensy 3
我在Arduino/Teensy环境中有一个C++类,该类在".h"文件中定义。在".cpp"文件中,我正在尝试使用一些代码进行"新放置"。我收到以下错误:
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*)'
所以看起来问题在于,在 Teensy 核心库中,没有定义新的位置 - 运算符只期望一个参数,而不是两个参数。
如果我像这样在".h"文件中定义我自己的放置 new 实现并将其包含在上述类的头文件中:
#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
它似乎有效,但前提是我在头文件内的方法中使用放置 new。如果我将代码移出标头并移入".cpp"实现文件,我会收到一个类似的错误,即只需要一个参数。
有没有办法解决这个问题?
我发现解决这个问题的最直接方法是简单地打开
/opt/arduino-1.6.7/hardware/teensy/avr/cores/teensy3/new.h
并把原型
void *operator new(size_t size, void *ptr);
void operator delete(void *obj, void *alloc);
那里对运算符进行多次重载,然后在关联的".cpp"文件中使用函数。
不知道为什么一开始就不包括在内...
相关文章:
- 使用新行和不使用新行读取文件
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 遇到新行时,有没有办法停止istream_iterator
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 计算缩放多边形的比例,得到给定的多边形面积
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 当一个新对象被分配到它的地址时,对象是否必须被销毁
- QwtPlot具有相等的轴和自动缩放
- 如何在directx/c++中进行平移/缩放操作
- 模板元编程:如何将参数包组合成新的参数包
- 我可以将新的 std::tuple 放入内存映射区域,并在以后读回吗?
- C++,打开和编辑文本文件中的数字数据,并将结果放入新文件中
- 将 int 放入 char 数组是否在法律上需要新的放置
- 如何将未排序数组的排序索引放入新数组中
- 使用新放置与 Teensy 3.
- 视觉计数重复,并将唯一条目放入数组 [C++] 中的新数组中
- c++将行getline放入数组中,用新行分隔
- 读取文件夹中的所有文件,然后将每个文件及其匹配项放入新的excel文档中.是否可以用c++
- Visual Studio将新文件放入错误的目录中