Pages: [1]   Go Down
Author Topic: C++ new, libraries  (Read 2401 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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:
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?
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

New Zealand
Offline Offline
God Member
*****
Karma: 0
Posts: 999
Arduino pebbles
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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! smiley
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I will try again,
thanks a lot
fabrizio
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

nevermind, I e-mailed the author and the library is now useable for 0017  smiley-grin
Logged

Pages: [1]   Go Up
Jump to: