我在Arduino/Teensy环境中有一个在".h“文件中定义的C++类。在".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*)'所以看起来问题是在很小的核心库中,位置new没有被定义-运算符只需要一个参数,而不是两个。
如果我像这样在".h“文件中定义自己的放置新实现,并将其包含在上面类的头文件中:
#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“实现文件中,我会得到一个类似的错误,即只需要一个参数。
有没有办法解决这个问题?
发布于 2016-02-18 11:46:18
我发现解决这个问题的最直接的方法就是
/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“文件中的函数。
不知道为什么一开始就没有包括在内……
https://stackoverflow.com/questions/35465405
复制相似问题