📬❄️📦Adafruit Holiday Shipping Deadlines 2019: Please place all UPS 3 Day orders by 11am ET Friday December 13 📬❄️📦
0

Class Struggle
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Class Struggle

by pictiPig on Mon Oct 13, 2008 8:09 am

I'm trying to turn some of my spaghetti code into a sensible class structure.

So, I create a folder inside my libraries folder and add some classes. So far so good, I can #include the header and instantiate whatever class I've made.

BUT, if I would appear to be unable to include another class within my own classes.

Here's a rather frivolous cut down example :

|=========================
pigDebug.h
|=========================
Code: Select all | TOGGLE FULL SIZE
#ifndef __pigDebug__
#define __pigDebug__
#include "AFSoftSerial.h"
class pigDebug :public AFSoftSerial
{
public:
   pigDebug(int rx,int tx);
   void start(long baud);
   void out(char* msg);
};
#endif



|=========================
pigDebug.cpp
|=========================
Code: Select all | TOGGLE FULL SIZE
#include "pigdebug.h"
#include "hardwareserial.h"

pigDebug::pigDebug(int rx,int tx)
   :AFSoftSerial(rx,tx)
{
}
void pigDebug::start(long baud)
{
   begin(baud);
}
void pigDebug::out(char* msg)
{
   print(msg);
}


Attempting to include this in a test project gives me :
hardware\libraries\pigtools/pigdebug.h:3:26: error: AFSoftSerial.h: No such file or directory


I can force the compiler to see AFSoftSerial.h by changing my #include to
''#include "../AFSoftSerial/AFSoftSerial.h", which allows me to compile ok, but it looks like the compiler is unable to track down the .cpp code, since when I attempt to upload to my arduino I get the following error :
\pigdebug.cpp:14: undefined reference to `AFSoftSerial::print(char const*)'


I can make the problem go away for a self contained library like AFSoftSerial, by copying AFSoftSerial.h and .cpp into my own library folder, but that's kind of defeating the reusability that I'm looking for in a class. And when I attempt to create a class which requires, for example, wire.h there are dependencies that get lost in the move. I'd end up re-creating everthing for every library I make. That can't be right!

Am I pushing for too much from the IDE? Have I missed something obvious?


Scott
pictiPig
 
Posts: 8
Joined: Mon Jul 07, 2008 12:50 pm
Location: Scotland

Re: Class Struggle

by adafruit on Tue Oct 14, 2008 5:18 pm

afaik the ide doesnt have a smart enough compiling setup to know about all of the directories. so if you want to extend a class you will have to just copy the files and/or do something at the filesystem level with redirected files (dunno if that would work tho)

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: Class Struggle

by pictiPig on Tue Oct 14, 2008 5:38 pm

I've not had as much time to play with this as I'd like, but so far I've managed to trick the compiler into preparing everything by including the necessary files in the main .pde file, so for the above example,
#include "AFSoftSerial.h" at the top of the .pde seems to pull everything that's needed in.

Hopefully I'll be able to have a go at something more complicated soon, and I'll report my findings. If I end up having to have a bunch of seemingly pointless includes at the top of the main src file it'd not be too much of a hardship I suppose...

Unfortunately I'm devving this in Windows, so there's not much scope for clever filesystem redirections - I might have a go at that later though - good idea.


Scott
pictiPig
 
Posts: 8
Joined: Mon Jul 07, 2008 12:50 pm
Location: Scotland

Please be positive and constructive with your questions and comments.