Compiler can't see my overloaded new() operator.

Started by
4 comments, last by garyfletcher 18 years, 10 months ago
I am trying to complie and test a memory manager but 1 of my overloaded operators isn't being seen and I'm not too sure why:

int main()
{
                     .
                     .
                   Stuff
                     .
                     .
    // This allocation should automatically go into their hierarchical heaps
    GameEntity * pEntity = new GameEntity;    /* works ok */
    GameEntity * pCamera = new GameCamera;    /* works ok */
    GameEntity * pCamera2 = new GameCamera;   /* works ok */
    const int numActors = 20;
    GameEntity * pActor[numActors];
    for (int i=0; i<numActors; ++i)
        pActor = new GameActor;            /* works ok */

    // This is a custom allocation in a specific heap
    Heap * pHeap = HeapFactory::CreateHeap("CustomHeap");
    int * pBuffer = new (pHeap) int[512]; /* This line is causing problem */
                     .
                     .
                More stuff
                     .
                     .
    return 0;
}


overloaded operator stuff: Header File

#include "HeapFactory.h"
#include <cstddef>

void * operator new (size_t size, Heap* pHeap); /* Not being seen */
void * operator new (size_t size); /* this is ok though */
void operator delete (void * pMem, size_t size); 
                     .
                     .
                More stuff
                     .
                     .
#endif


Source file:

#include "MemoryMgr.h"
#include "Heap.h"
#include "HeapFactory.h"
#include <assert.h>

void* operator new (size_t size, Heap* pHeap) /* Not being seen */
{
    assert(pHeap != NULL);
    return pHeap->Allocate(size);
}

void* operator new (size_t size) /* Is being seen */
{
    return operator new (size, HeapFactory::GetDefaultHeap() );
}

void operator delete (void * pMem) 
{
    if (pMem != NULL)
        Heap::Deallocate(pMem);    
}


complier error:

main.cpp: In function `int main()':
main.cpp:27: error: no matching function for call to `operator new [](unsigned int, Heap*&)'
<internal>:0: note: candidates are: void* operator new [](unsigned int)


The code is actually from a book called "C++ for Games Programmers" and was in a visual C++ project. I'm using Dev-c++, am not too sure if that would be an issue as I created a new project and added the source files. Can anyone see what the problem is?
Gary.Goodbye, and thanks for all the fish.
Advertisement
#include <new> ?

You need it when you start overloading operator new and operator delete.

Also, operator new (for single elements) and operator new[] (for arrays) are two different operators, which must be overloaded separately. Providing one does not generate the other.

GameEntity * pCamera2 = new GameCamera; uses operator new
int * pBuffer = new (pHeap) int[512]; uses operator new[]

Finally, consider overloading a class' operator new (etc) instead of the global one, and deriving your objects from that class. It's cleaner.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Cheers Fruny....that got rid of that problem.

The source I'm using was originally a VC++ project, I suppose that the project itself could have the new header file specified?

Anyway, thanks.
Gary.Goodbye, and thanks for all the fish.
Quote:Original post by garyfletcher
int * pBuffer = new (pHeap) int[512]; /* This line is causing problem */



I don't see an overloaded version of array new and array delete.

Also note that there are two versions of all global operators new/delete, one that throws std::bad_alloc exception and one which has a nothrow declaration, e.g.:

void* operator new(std::size_t) throw (std::bad_alloc);void* operator new(std::size_t, const std::nothrow_t&) throw();void operator delete(void*) throw();void operator delete(void*, const std::nothrow_t&) throw();


When redefining (not overloadding) operators new/new[]/delete/delete[] you don't need to redeclare them just include header new and redefine them.

Quote:Original post by garyfletcher
The source I'm using was originally a VC++ project, I suppose that the project itself could have the new header file specified?


You actually expect code from books, especially game programming books, to be legal C++?

Fool. [smile]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
lol...thanks for that!!!

Well I live in a world full of happiness and joy, where everyday is the first day of spring and poverty and war no longer exist.

Yep...that's the UK alright;)
Gary.Goodbye, and thanks for all the fish.

This topic is closed to new replies.

Advertisement