Pages: [1]   Go Down
Author Topic: Simple Inheritance Issue  (Read 1458 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
#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:
#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:
/*
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:
#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!
« Last Edit: February 12, 2010, 04:36:07 pm by burkeyturkey » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 178
Posts: 12288
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Code:
#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
« Last Edit: February 12, 2010, 04:49:21 pm by bcook » Logged

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

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

Pages: [1]   Go Up
Jump to: