Go Down

Topic: Having problems making library (Read 348 times) previous topic - next topic

Volta500

Hello all,
I am trying to make a library for the DFRobot LCD shield. After reading the tutorial on the Arduino site I came up with this code:
Code: [Select]
/*
  LCDShield.cpp
*/
#include "LCDShield.h"

LCDShield::LCDShield() : LiquidCrystal(8, 9, 4, 5, 6, 7){
  ; // nothing to construct
}

char LCDShield::button(){
  uint16_t inputVal=analogRead(A0);
  delay(2);
  char c='0';
  switch(inputVal){
    case 1023:
      c= -1;
      break;
    case 742:
    case 741:
    case 743:
      c= 's';
      break;
    case 141:
    case 142:
    case 143:
      c= 'u';
      break;
    case 328:
    case 327:
    case 326:
      c= 'd';
      break;
    case 503:
    case 504:
    case 505:
      c= 'l';
      break;
    case 0:
    case 1:
      c= 'r';
      break;
    default:
      c= '0';
      break;
  }
  return c;
}


Code: [Select]
/*
  LCDShield.h
*/
#include "Arduino.h"
#include <LiquidCrystal.h>

#ifndef LCDShield_h
#define LCDShield_h

class LCDShield : public LiquidCrystal{
  public:
    LCDShield();
    char button();
};

#endif



And a very quick program to test it:

Code: [Select]
#include <LCDShield.h>

LCDShield lcd();

void setup(){
  Serial.begin(9600);
}


void loop(){
  char input = lcd.button();
  Serial.println(input);
  delay(200);
}


But when I try to compile, this error came up:
Code: [Select]
In file included from test1.ino:1:
/Users/Pelle/Dropbox/Electronics/Microcontrollers/Arduino Projects/libraries/LCDShield/LCDShield.h:10: error: expected class-name before '{' token
test1.ino: In function 'void loop()':
test1:13: error: request for member 'button' in 'lcd', which is of non-class type 'LCDShield ()()'


I searched the web but never got a resounding solution for this problem. I guess it is something with me trying to include the liquid crystal class and something going wrong there. Or it is just a stupid beginners error  :smiley-roll-blue:

Can anyone lend me a hand with this issue?
Im using an Arduino Uno if it matters.

Thank you

PaulS

Code: [Select]
LCDShield lcd();
What do you think this is doing? It isn't, but I'm interested in what you think it is doing.

Loose the parentheses.

lcd is NOT an instance of LCDShield when this code gets done.

Volta500

I thought I was initializing the class and making the functions in it available through lcd. It probably is a remnant of learning python first, where you do use parentheses to initialize a class.
Thanks for pointing it out.

But this only solves halve of the problem: The error
Code: [Select]

In file included from test1.ino:1:
/Users/Pelle/Dropbox/Electronics/Microcontrollers/Arduino Projects/libraries/LCDShield/LCDShield.h:10: error: expected class-name before '{' token

Remains.
There is something wrong with my class definition (class LCDShield : public LiquidCrystal) but I can't figure out what.

PaulS

#3
Jan 07, 2013, 03:29 pm Last Edit: Jan 07, 2013, 03:37 pm by PaulS Reason: 1
You are trying to do the same thing every new library developer wants to do - hide the use of one library in another. You can not do that.

You can use one library (LiquidCrystal) in another (LCDShield), but you must tell the sketch that you are using both of them.

Go Up