17

I cannot seem to get my placement new to work for some reason. Based on this question, I have set this up correctly.

However, I continue to get the error:

'operator new' : function does not take 2 arguments

Here is my code:

char * p = new char [sizeof(Node) * 5];
Node* node = new(p) Node();

where Node is a linked list node. I have tried to simplify this down based on the other stack overflow question, and I am still getting the same error:

char *buf  = new char[sizeof(int)]; // pre-allocated buffer
int *p = new (buf) int;

Does anyone know why I am having this issue?

Any help is greatly appreciated!

PS, this works:

Node* node = new Node();
Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
Serguei Fedorov
  • 7,763
  • 9
  • 63
  • 94
  • 1
    possible duplicate of [No matching function for call to operator new](http://stackoverflow.com/questions/3156778/no-matching-function-for-call-to-operator-new) – CB Bailey Nov 07 '13 at 08:59

1 Answers1

36

Most likely, you didn't include <new>. You need that for the declarations of the standard forms of placement-new.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • 1
    Wow the little things you can miss! Thank you! – Serguei Fedorov Nov 07 '13 at 08:45
  • @Mike Seymour - +1 for the great answer. A question regarding a case I'm facing: I'm using Microsoft C/C++ compiler version 19.13.26128 (for x86), and in my case, in order to eliminate the error in the question, I needed to use add the global scope before the new keyword. So utilizing one of the examples from the code of the question, I wrote: Node* node = ::new (p) Node(); - Do you have an idea why it is like this ? (also note that the compiler does not asks me to include the header file). Thanks !! – Guy Avraham Apr 04 '18 at 12:28
  • It might be worth to mention that another case where this would happen is if you are constructing a class via placement-new that has a **custom new operator**. Cost me a good amount of time trying to include everywhere when I had this problem. – Vinz Jul 18 '18 at 11:05