system
July 15, 2008, 3:15pm
1
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:
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,
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:
Trackpad::Trackpad(int clk, int data)
{
static PS2 mVar(clk,data);
mouse = &mVar;
but I get a weird weird error:
/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
July 15, 2008, 4:01pm
2
If you only need one instance, you can just make the member of type PS2 rather than (PS2 *).
system
July 16, 2008, 2:28am
3
but I get a weird weird error:
/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.
system
July 17, 2008, 4:23pm
4
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...
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:
#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!
system
August 17, 2009, 4:30pm
5
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
system
August 18, 2009, 12:14pm
6
That -is- a strange error message. Are you sure that's the entire message?
system
August 18, 2009, 1:03pm
7
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
system
August 18, 2009, 1:16pm
8
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.
system
August 18, 2009, 1:29pm
9
I will try again,
thanks a lot
fabrizio
system
November 5, 2009, 8:56am
10
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:
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:
hardware\libraries\Trackpad/Trackpad.h:27: error: extra qualification 'Trackpad::' on member 'sendComSeq'
If anyone has a solution I would highly appreciate it!
system
November 5, 2009, 3:17pm
11
nevermind, I e-mailed the author and the library is now useable for 0017