Go Down

Topic: C++ new, libraries (Read 2556 times) previous topic - next topic

joerick

I'm writing a library that uses another library for lower-level stuff. Basically, I want to have an instance of this class and a pointer to it to talk to it.

So here's my class definition:
Code: [Select]
class Trackpad
{
     public:
           Trackpad(int clk, int data);
           ~Trackpad();
           packet_t * getNewPacket();
     private:
           void Trackpad::sendComSeq(byte arg, boolean setMode = false);
           PS2 * mouse;
           packet_t packet;
};


so, you can see the pointer (PS2 is the class I'm using).

Then, in my initialiser,

Code: [Select]
Trackpad::Trackpad(int clk, int data)
{
     mouse = new PS2(clk,data);

     //Blah blah blah
}

Trackpad::~Trackpad()
{
     delete mouse;
}


Compiler complains that it doesn't know how to 'new' something.

So, how do I do this? Can I malloc the space and refer to it as a class?

I also tried this:

Code: [Select]
Trackpad::Trackpad(int clk, int data)
{
     static PS2 mVar(clk,data);
     
     mouse = &mVar;


but I get a weird weird error:
Quote
/Users/joerick/Desktop/arduino-0011/hardware/libraries/Trackpad/Trackpad.cpp:16: undefined reference to `__cxa_guard_acquire'

/Users/joerick/Desktop/arduino-0011/hardware/libraries/Trackpad/Trackpad.cpp:16: undefined reference to `__cxa_guard_release'


What do you think? Can I subclass perhaps instead?

mellis

If you only need one instance, you can just make the member of type PS2 rather than (PS2 *).  

follower

Quote

but I get a weird weird error:
Quote
/Users/joerick/Desktop/arduino-0011/hardware/libraries/Trackpad/Trackpad.cpp:16: undefined reference to `__cxa_guard_acquire'

/Users/joerick/Desktop/arduino-0011/hardware/libraries/Trackpad/Trackpad.cpp:16: undefined reference to `__cxa_guard_release'


What do you think?

If you do a Google for those function names you should find an explanation.

--Phil.

joerick

Quote
If you only need one instance, you can just make the member of type PS2 rather than (PS2 *).  


Except that I need to give the class some constructor variables, and don't have these when my class opens...

Quote

If you do a Google for those function names you should find an explanation.


Found the thread you're referring to, and the fix lies here.

The solution, for anyone who's interested, is a file like this:
Code: [Select]
#ifndef cppfix
#define cppfix

__extension__ typedef int __guard __attribute__((mode (__DI__)));

void * operator new(size_t size);
void operator delete(void * ptr);

int __cxa_guard_acquire(__guard *g) {return !*(char *)(g);};
void __cxa_guard_release (__guard *g) {*(char *)g = 1;};
void __cxa_guard_abort (__guard *) {};

void * operator new(size_t size)
{
 return malloc(size);
}

void operator delete(void * ptr)
{
 free(ptr);
}

#endif


Include that, and you can declare static classes, and use new and delete.

Great! :)

Vincent_ch

Dear joerick,
I am having exactly the same problem, I do not seem to be able to use new and delete in a cpp class that I have made for my Arduino Mega. Googling here and here I ended up here.
When you say "Include that, and you can declare static classes, and use new and delete."

do you mean to write:

#include "cppfix.h"
in the .pde arduino source file (the one that contains the setup() and loop())?
and also include the cppfix.h file (with in it what you suggested) in the Arduino sketch?

I did that but I still get the strange error message :

In function 'void operator delete(void*)':


thank you for your help
fabrizio

joerick

That -is- a strange error message. Are you sure that's the entire message?

Vincent_ch

well yes, pretty sure. I am using the Arduino 0016 and that is what appears at the end of the IDE in red. I have now changed all delete and new with free and malloc and things are ok now... I wish however to know how to be able to use new and delete.
Could you just clarify how and where to include the cppfix file .
thanks
fabrizio

joerick

No idea what's up with that. You should include the cppfix.h at the top of each .cpp file. Perhaps you didn't copy and paste the entire file above?

I haven't used this with a recent version of Arduino, but it should still work.

Vincent_ch

I will try again,
thanks a lot
fabrizio

teamtomtom

maybe a big bump, but I'm trying to get this library to work, but the arduino software 0017 keeps giving me different compile errors:
Quote
C:\Program Files\arduino-0017\hardware\libraries\Trackpad\cppfix.cpp:13: error: declaration of 'operator new' as non-function

C:\Program Files\arduino-0017\hardware\libraries\Trackpad\cppfix.cpp:13: error: 'size_t' was not declared in this scope

C:\Program Files\arduino-0017\hardware\libraries\Trackpad\cppfix.cpp:20: error: declaration of 'operator new' as non-function

C:\Program Files\arduino-0017\hardware\libraries\Trackpad\cppfix.cpp:20: error: 'size_t' was not declared in this scope

C:\Program Files\arduino-0017\hardware\libraries\Trackpad\cppfix.cpp: In function 'void operator delete(void*)':

C:\Program Files\arduino-0017\hardware\libraries\Trackpad\cppfix.cpp:27: error: 'free' was not declared in this scope



I tried 0016 and 0015 too, but they also throw an error:
Quote
hardware\libraries\Trackpad/Trackpad.h:27: error: extra qualification 'Trackpad::' on member 'sendComSeq'

If anyone has a solution I would highly appreciate it!

teamtomtom

nevermind, I e-mailed the author and the library is now useable for 0017  :D

Go Up