| View previous topic :: View next topic |
| Author |
Message |
DaVajj
Joined: 19 Apr 2006 Posts: 9
|
Posted: Wed Apr 19, 2006 12:47 am Post subject: C++ on PSP |
|
|
I've just recently managed to successfully run the toolchain script and begun programming for the PSP, although most examples for the PSP is in C.
I tried creating a class and that worked, but when i was gonna do a singleton class for pad-input i got a link error on the 'new' keyword.
| Code: | CPad.o: I funktionen "CPad::Instance()":
CPad.cpp:(.text+0x30): undefined reference to `operator new(unsigned int)'
collect2: ld returned 1 exit status
make: *** [hello.elf] Fel 1 |
CPad.cpp:
| Code: | #include "CPad.h"
CPad * CPad::pInstance = NULL;
CPad * CPad::Instance()
{
if( pInstance == NULL )
pInstance = new CPad;
return pInstance;
} |
CPad.h
| Code: | #ifndef H_CPAD
#define H_CPAD
//Includes
#include <pspctrl.h>
class CPad
{
public:
static CPad * Instance();
~CPad() { }
void update() { sceCtrlReadBufferPositive( &pad, 1 ); }
bool keyPressed(unsigned int keyCode) { return pad.Buttons & keyCode; }
private:
CPad() { pad.Buttons = 0; }
static CPad * pInstance;
//Pad data
SceCtrlData pad;
};
#endif
|
Any help on how to fix/circumvent this would be appreciated. |
|
| Back to top |
|
 |
__count
Joined: 23 Mar 2006 Posts: 22
|
Posted: Wed Apr 19, 2006 1:04 am Post subject: |
|
|
easy fix:
#include <memory.h>
void *operator new(size_t size){ return malloc(size); }
void operator delete(void *ptr){ free(ptr); }
an alternative is to include the C++ library
Also this has been asked many times, please use the search first next time. |
|
| Back to top |
|
 |
DaVajj
Joined: 19 Apr 2006 Posts: 9
|
Posted: Wed Apr 19, 2006 3:05 am Post subject: |
|
|
First of, thanks for your reply.
Second: I did search. For "new c++" (without "") and "new", but no results. But iIguess I didn't search enough >_<.
(Since i've already started a whole new thread for this, why not keep using it:)
Which library is the "C++ library" ?
And I guess I should just add it to my Makefile, like any other library? |
|
| Back to top |
|
 |
danzel
Joined: 04 Nov 2005 Posts: 182
|
Posted: Wed Apr 19, 2006 5:56 am Post subject: |
|
|
add " -lstdc++ " to the end of your LIBS= line in your makefile.
Also, your usage of new is slightly incorrect, you need to call the constructor as follows:
| Code: | | pInstance = new CPad(); |
Hope that helps :) |
|
| Back to top |
|
 |
DaVajj
Joined: 19 Apr 2006 Posts: 9
|
Posted: Wed Apr 19, 2006 6:16 am Post subject: |
|
|
Oh, right, forgot the parenthesis there. Was just quickly throwing it all together. Thanks a lot!
Edit: Wait a minute. Do you really have to have the parenthesis there? The constructor doesn't take any arguments. To create a standard C++ string on the heap you just do this right?
Since the constructor doesn't take any arguments. (And std::string is a class). Anyways, i'll try out the lib later.
Edit2: The lib worked great! And parenthesis was not required. Back to coding. |
|
| Back to top |
|
 |
danzel
Joined: 04 Nov 2005 Posts: 182
|
Posted: Wed Apr 19, 2006 12:25 pm Post subject: |
|
|
Ah, didn't know new allowed for that behaviour.
It most likely calls the default constructor. |
|
| Back to top |
|
 |
|