forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C++ on PSP

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
DaVajj



Joined: 19 Apr 2006
Posts: 9

PostPosted: Wed Apr 19, 2006 12:47 am    Post subject: C++ on PSP Reply with quote

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
View user's profile Send private message
__count



Joined: 23 Mar 2006
Posts: 22

PostPosted: Wed Apr 19, 2006 1:04 am    Post subject: Reply with quote

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
View user's profile Send private message
DaVajj



Joined: 19 Apr 2006
Posts: 9

PostPosted: Wed Apr 19, 2006 3:05 am    Post subject: Reply with quote

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
View user's profile Send private message
danzel



Joined: 04 Nov 2005
Posts: 182

PostPosted: Wed Apr 19, 2006 5:56 am    Post subject: Reply with quote

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
View user's profile Send private message
DaVajj



Joined: 19 Apr 2006
Posts: 9

PostPosted: Wed Apr 19, 2006 6:16 am    Post subject: Reply with quote

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:
new std::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.
Back to top
View user's profile Send private message
danzel



Joined: 04 Nov 2005
Posts: 182

PostPosted: Wed Apr 19, 2006 12:25 pm    Post subject: Reply with quote

Ah, didn't know new allowed for that behaviour.
It most likely calls the default constructor.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group