|
-
May 25th, 2009, 09:27 AM
#1
Instantiating a class using 'new' does not work.
Hey.
I'm currently doing a uni assignment where I, among other things, must take C code and convert it to a C++ project with an object-oriented design.
I have created a class called CPlayer, which I try to create an instance of like so:
Code:
CPlayer *newNode = new CPlayer();
This compiles without errors but when it attempts to execute the compiled EXE it gives me the error:
Unable to start program '%CSIDL_PROGRAM_FILES%\Donuts2\Donuts2.exe'. An error occurred that usually indicates a corrupt installation (code 0x8007007e). If the problem persists, repair your Visual Studio installation via 'Add or Remove Programs' in Control Panel.
I have no idea why this happens. A workaround has been to do this:
Code:
CPlayer *newNode = (CPlayer*) LocalAlloc( LPTR, sizeof( CPlayer ) );
However this does not invoke the constructor, of course.
What could be causing this? The class is fairly simple and I've tried commenting out everything in its constructor so Ive ruled out the fact that there are any errors there.
An important thing to mention is that at another place in my code I am creating an instance of another class using the 'new' keyword successfully.
If you need to see the class definition tell me and I'll post it, or a portion of it, but I'd rather not do it if its not needed, as this is after all a uni assignment.
Edit: I have now discovered that no class seems to be "instantiate-able" inside the methods of my "Game" class. However, if I try to instantiate any of these classes outside of the "Game" class (Such as in the WinMain "Entry point" method), it works like a charm.
To clarify, this does not work:
Code:
#include "Game.h"
void Game::DoSomething(){
CPlayer *newNode = new CPlayer();
}
but this works:
Code:
#include "Game.h"
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine,
int nCmdShow ){
CPlayer *newNode = new CPlayer();
}
(Game.h includes CPlayer.h)
Last edited by Atheist; May 25th, 2009 at 09:47 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|