Jump to content
  • Advertisement
Sign in to follow this  

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

This topic is 4737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!