Go Down

Topic: Simple Inheritance Issue (Read 2038 times) previous topic - next topic

Burkey Turkey

Feb 12, 2010, 10:33 pm Last Edit: Feb 12, 2010, 10:36 pm by burkeyturkey Reason: 1
Hey all,

I've been using Arduinos (Duem. 368) for almost 2 years now, and I'm trying to develop some libraries to help the other members in my project control our robots more easily.  For communication between Arduinos, we have standardized ascii "packets" that we have defined, but i'm trying to wrap everything involving communication up into something more high level for the new users.  I wrote a simple "Packet" class, and then each of our robots (we make robotic instruments) has a unique subclass of Packet which holds information about individual notes.  I've asked everyone i know who is involved with cpp at any level, and have had no luck in finding a solution to the compile error I get:

Code: [Select]
D:\Documents\arduino 18\arduino-0018\libraries\ROPbassdrum\ROPbassdrum.cpp:4: error: new types may not be defined in a return type

D:\Documents\arduino 18\arduino-0018\libraries\ROPbassdrum\ROPbassdrum.cpp:4: note: (perhaps a semicolon is missing after the definition of 'ROPbassdrum')

D:\Documents\arduino 18\arduino-0018\libraries\ROPbassdrum\ROPbassdrum.cpp:4: error: return type specification for constructor invalid


It sounds like it doesn't like my constructor, but I don't even have control over the return type of constructors anyway really...so I don't know what to do.

Here are the h and cpp file for the Superclass: ROPacket

Code: [Select]
#ifndef ROPacket_h
#define ROPacket_h

#include "WProgram.h"

class ROPacket
{
 public:
   ROPacket(byte myID, byte myType);
   byte getID();
   byte getType();
   char* toString();
   void freePacket();
 protected:
   byte ID;
   byte type;
   char* str;
};

#endif

Code: [Select]

#include "WProgram.h"
#include "ROPacket.h"

ROPacket::ROPacket(byte myID, byte myType)
{
 ID=myID;
 type=myType;
}

byte ROPacket::getID()
{
 return ID;
}

byte ROPacket::getType()
{
 return type;
}

char* ROPacket::toString()
{
 str = (char*)malloc(4*sizeof(char));
 str[0] = type;
 str[1] = ID;
 str[2] = type;
 str[3] = '\0';
 return str;
}

void ROPacket::freePacket()
{
 if(str != NULL){
   free(str);
 }
}


Here are the h and cpp files for the Subclass: ROPbassdrum

Code: [Select]

/*
ROPbassdrum.h - Subclass of ROPacket
*/

#ifndef ROPbassdrum_h
#define ROPbassdrum_h

#include "WProgram.h"
#include "ROPacket.h"

class ROPbassdrum : public ROPacket
{
 public:
   ROPbassdrum(byte myID, byte myType, byte myHitType);
   byte getHitType();
   char* toString();
 private:
   byte hitType;
}

#endif

Code: [Select]

#include "WProgram.h"
#include "ROPbassdrum.h"

ROPbassdrum::ROPbassdrum(byte myID, byte myType, byte myHitType) : ROPacket(myID, myType)
{
 hitType = myHitType;
}

byte ROPbassdrum::getHitType()
{
 return hitType;
}

char* ROPbassdrum::toString()
{
 str = (char*)malloc(5*sizeof(char));
 str[0] = type;
 str[1] = ID;
 str[2] = hitType;
 str[3] = type;
 str[4] = '\0';
}


I haven't been able to find any good resources on inheritance in Arduino, so i could really use some feedback on this.  Thanks community!

Coding Badly

#1
Feb 12, 2010, 10:48 pm Last Edit: Feb 12, 2010, 10:49 pm by bcook Reason: 1

Does this help (append a semi-colon)...

Code: [Select]
#define ROPbassdrum_h

#include "WProgram.h"
#include "ROPacket.h"

class ROPbassdrum : public ROPacket
{
 public:
   ROPbassdrum(byte myID, byte myType, byte myHitType);
   byte getHitType();
   char* toString();
 private:
   byte hitType;
}[glow];[/glow]  // <<<

#endif

Burkey Turkey

Thanks so much! Thats really tricky.  I was looking in the cpp file the entire time.  It worked, now my library compiles.  I owe you one.

Go Up