C++ on PSP

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
DaVajj
Posts: 9
Joined: Wed Apr 19, 2006 12:39 am

C++ on PSP

Post by DaVajj »

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: Select all

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: Select all

#include "CPad.h"

CPad * CPad::pInstance = NULL;

CPad * CPad::Instance()
{
	if( pInstance == NULL )
		pInstance = new CPad;
	return pInstance;
}
CPad.h

Code: Select all

#ifndef H_CPAD
#define H_CPAD

//Includes
#include <pspctrl.h>

class CPad
&#123;
	public&#58;
		static CPad * Instance&#40;&#41;;
		~CPad&#40;&#41; &#123; &#125;

		void update&#40;&#41; &#123; sceCtrlReadBufferPositive&#40; &pad, 1 &#41;; &#125;
		bool keyPressed&#40;unsigned int keyCode&#41; &#123; return pad.Buttons & keyCode; &#125;

	private&#58;
		CPad&#40;&#41; &#123; pad.Buttons = 0; &#125;
		static CPad * pInstance;

		//Pad data
		SceCtrlData pad;
&#125;;

#endif
Any help on how to fix/circumvent this would be appreciated.
__count
Posts: 22
Joined: Thu Mar 23, 2006 8:40 pm

Post by __count »

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.
DaVajj
Posts: 9
Joined: Wed Apr 19, 2006 12:39 am

Post by DaVajj »

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?
danzel
Posts: 182
Joined: Fri Nov 04, 2005 11:03 pm

Post by danzel »

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: Select all

pInstance = new CPad&#40;&#41;;
Hope that helps :)
DaVajj
Posts: 9
Joined: Wed Apr 19, 2006 12:39 am

Post by DaVajj »

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?

Code: Select all

new std&#58;&#58;string;
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.
danzel
Posts: 182
Joined: Fri Nov 04, 2005 11:03 pm

Post by danzel »

Ah, didn't know new allowed for that behaviour.
It most likely calls the default constructor.
Post Reply