2

I have a C++ class in the Arduino/Teensy environment which is defined in a ".h" file. Within the ".cpp" file I'm attempting to do "placement new" with some code. I'm getting the following error:

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

So it looks like problem is that in the Teensy core libraries placement new isn't defined - the operator is expecting only a single argument, not two.

If I define my own implementation of placement new in a ".h" file like so and include it in the header file of the above class:

#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

it seems to work, but only if I use placement new in a method within the header file. I get a similar error about only a single argument being expected if I move the code out of the header and into the ".cpp" implementation file.

Is there a way to resolve this?

MattyZ
  • 1,541
  • 4
  • 24
  • 41
  • 1
    `static_cast` is suspicious, and your operator new/delete would need to be `inline` if there are in header. (And I think they should be in only cpp file). – Jarod42 Feb 17 '16 at 19:05
  • @Jarod42 The reason I'm trying to do that is that there are several different classes that derive from AudioStream, and I want to upcast them to their parent class, so within the class I am defining here I only need to keep one pointer (of type AudioStream) to the buffer where they are placed, rather than keep a separate pointer for each type and a bunch of logic to figure out where the pointer returned from "new" should be put – MattyZ Feb 17 '16 at 19:22
  • basically the problem is that in the library I'm using, in class "AudioStream" only the "update" function for the stream has been declared "virtual", so if I'm going to store a pointer to the superclass for all subclasses of "AudioStream" I believe I have to downcast the pointer every time I want to call a method other than that for one of its subclasses – MattyZ Feb 17 '16 at 19:27
  • Do you know **why** you do placement new ? what is `_waveform` and how has it been initialized ? – Jarod42 Feb 17 '16 at 19:43
  • As in the accepted answer to this question: http://stackoverflow.com/questions/35371369/finding-out-the-largest-size-of-several-objects-for-placement-new _waveform is a type of one of the subclasses of AudioStream within the union in the private variable section, and then I'm using the following (_current tone is a pointer of type AudioStream): `_current_tone = static_cast(new (&_waveform) AudioSynthWaveform);` – MattyZ Feb 17 '16 at 20:19

1 Answers1

1

I found the most straightforward way to resolve this is to simply open up

/opt/arduino-1.6.7/hardware/teensy/avr/cores/teensy3/new.h

And put the prototypes

void *operator new(size_t size, void *ptr);

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

there to multiple overload the operator, and then the functions in the associated ".cpp" file.

Not sure why that wasn't included to begin with...

MattyZ
  • 1,541
  • 4
  • 24
  • 41