「.h」ファイルで定義されている Arduino/Teensy 環境に 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*)'
問題は、Teensy コア ライブラリで配置 new が定義されていないことです。演算子は、2 つではなく、1 つの引数のみを想定しています。
「.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
うまくいくようですが、ヘッダーファイル内のメソッドで新しい配置を使用した場合に限ります。コードをヘッダーから「.cpp」実装ファイルに移動すると、単一の引数のみが期待されるという同様のエラーが発生します。
これを解決する方法はありますか?