Pages: [1]   Go Down
Author Topic: Arduino Class "does not name a type"  (Read 1105 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello there, I am currently making a class in order to gain control over my LCD.
Here is the code:
Arduino_Marquee.h
Code:
#ifndef Arduino_Marquee_h
#define Arduino_Marquee_h

#include "Arduino.h"
#include <LiquidCrystal.h>

class Arduino_Marquee{
public:
  int max_string_length;
  int speed;
 
  char* text;
 
  Arduino_Marquee(int RS, int ENABLE, int D4, int D5, int D6, int D7, int LCD_COLLUMN, int LCD_ROW);
 
  void marquee_init();
  void marquee_draw();
private:
  LiquidCrystal lcd(int, int, int, int, int);

  int lcd_collumn;
  int lcd_row;

  int rs;
  int enable;
  int d4;
  int d5;
  int d6;
  int d7;
};

#endif

Arduino_Marquee.cpp
Code:
#include "Arduino.h"
#include "Arduino_Marquee.h"
#include <LiquidCrystal.h>

Arduino_Marquee::Arduino_Marquee(int RS, int ENABLE, int D4, int D5, int D6, int D7, int LCD_COLLUMN, int LCD_ROW){
  rs = RS;
  enable = ENABLE;
  d4 = D4;
  d5 = D5;
  d6 = D6;
  d7 = D7;

  lcd_collumn = LCD_COLLUMN;
  lcd_row = LCD_ROW;
}

void Arduino_Marquee::marquee_init(){
  lcd(rs, enable, d4, d5, d6, d7);
  lcd.begin(lcd_collumn, lcd_bar);
}

void Arduino_Marquee::marquee_draw(){
  for(int m = 0; m < lcd_collumn + max_string_length; m ++){
    for(int c = 0; c < lcd_row; c ++){
      for(int i = 0; i < max_string_length; i ++){
        lcd.setCursor(16 + i - m, c);
        lcd.print(text[c][i]);
      }
    }
    delay(500);
    lcd.clear();
  }
}

The error comes in "LiquidCrystal lcd(int, int, int, int, int);" , which is "LiquidCrystal does not name a type"
I need to control lcd inside my class.

So, I would expect some help from people here, regarding how can I make my class work.

Thank you in advance smiley
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is in your main sketch? Is that the only error message?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the intention of this line anyway?

Code:
private:
  LiquidCrystal lcd(int, int, int, int, int);   // <---- what is this doing?
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Member objects are declared like other variables

Code:
private:
    LiquidCrystal lcd;

The initialization of those objects happens at constructor time, with a syntax that looks like this:

Code:
Arduino_Marquee::Arduino_Marquee(int RS, int ENABLE, int D4, int D5, int D6, int D7, int LCD_COLLUMN, int LCD_ROW)
: lcd(RS, ENABLE, D4, D5, D6, D7) {

... more code ...

}

It's ok instead to save the rows and columns values and use them later when calling lcd.begin(). The reason is begin() is a "normal" method of the LiquidCrystal class, not its constructor.

Also a note about coding style: capital letters are usually used for constants and #defined symbols...

Anyway, I'd initialize the lcd object in the main sketch and just pass a pointer to it to the Marquee class, but that's just how I'd do it... :-)
Logged

Germany
Offline Offline
Faraday Member
**
Karma: 49
Posts: 2756
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why don't you just extend the LiquidCrystal class with your additional method ?

You can have a philosophical discussion  when it's appropriate to embed an object and when to derive from a base class,
IMHO here the approach class Arduino_Marquee:public LiquidCrystal{} is more appropriate.


I understand you rely on setting the public: char* text; variable prior to calling marquee_draw();,
which is intended to block your loop quite a while...   smiley-confuse

Inside marquee_draw() your
       lcd.print(text[c][ i  ]);
won't compile.
Instead of fixing the syntax error, I think it's a good idea to rethink your design in general, sorry  smiley-wink

Edit: hope it's clearer now. Fixed the  [ i ]  issue
« Last Edit: September 14, 2012, 04:13:49 am by michael_x » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the intention of this line anyway?

Code:
private:
  LiquidCrystal lcd(int, int, int, int, int);   // <---- what is this doing?

Okay, it is actually my bad habit for not commenting the code. smiley-sad
I am using the very basic LCD with 16 columns and 2 rows.
So according to LiquidCrystal.h (int, int, int, int, int) will refers to (rs, enable, d4, d5, d6, d7) respectively.

Member objects are declared like other variables

Code:
private:
    LiquidCrystal lcd;

The initialization of those objects happens at constructor time, with a syntax that looks like this:

Code:
Arduino_Marquee::Arduino_Marquee(int RS, int ENABLE, int D4, int D5, int D6, int D7, int LCD_COLLUMN, int LCD_ROW)
: lcd(RS, ENABLE, D4, D5, D6, D7) {

... more code ...

}

It's ok instead to save the rows and columns values and use them later when calling lcd.begin(). The reason is begin() is a "normal" method of the LiquidCrystal class, not its constructor.

Also a note about coding style: capital letters are usually used for constants and #defined symbols...

Anyway, I'd initialize the lcd object in the main sketch and just pass a pointer to it to the Marquee class, but that's just how I'd do it... :-)
I have change the .cpp files a bit to
Code:
(int RS, int ENABLE, int D4, int D5, int D6, int D7, int LCD_COLLUMN, int LCD_ROW):lcd(RS, ENABLE, D4, D5, D6, D7)
The error returned still the same which is "LiquidCrystal does not name a type"

Actually I want make a class to make a marquee effect for an LCD, so in my humble opinion it will be  still the same if I pass the initialization of the LCD outside with the marquee looping and just put the variable inside.

Why don't you just extend the LiquidCrystal class with your additional method ?

You can have a philosophical discussion  when it's appropriate to embed an object and when to derive from a base class,
IMHO here the approach class Arduino_Marquee:public LiquidCrystal{} is more appropriate.


I understand you rely on setting the public: char* text; variable prior to calling marquee_draw();,
which is intended to block your loop quite a while...   smiley-confuse

Inside marquee_draw() your
       lcd.print(text[c][ i  ]);
won't compile.
Instead of fixing the syntax error, I think it's a good idea to rethink your design in general, sorry  smiley-wink

Edit: hope it's clearer now. Fixed the  [ i ]  issue

I have tried the "extends" method in my code, when I compile it, it always return an error "Expected class name before '{' token".

About the lcd.print(text[c]); I have no problem compiling it in the previous sketch that I made, I mean firstly I made a sketch without a class, and it is works fine.

So any other idea?

I am sorry for the late reply but I am very thankful to your reply. smiley
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the intention of this line anyway?

Code:
private:
  LiquidCrystal lcd(int, int, int, int, int);   // <---- what is this doing?

Okay, it is actually my bad habit for not commenting the code. smiley-sad
I am using the very basic LCD with 16 columns and 2 rows.
So according to LiquidCrystal.h (int, int, int, int, int) will refers to (rs, enable, d4, d5, d6, d7) respectively.


Well, no it won't. How can it be passing in (rs, enable, d4, d5, d6, d7) when you just have (int, int, int, int, int)?

Quote
I have change the .cpp files a bit to

Code:
(int RS, int ENABLE, int D4, int D5, int D6, int D7, int LCD_COLLUMN, int LCD_ROW):lcd(RS, ENABLE, D4, D5, D6, D7)

The error returned still the same which is "LiquidCrystal does not name a type"

Can you post the amended code please.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The error returned still the same which is "LiquidCrystal does not name a type"

I suspect you still don't get the difference between constructing an object and calling a method on it...

Please have a look at the code here:

http://arduino.cc/en/Tutorial/LiquidCrystal

The following line creates an object named "lcd" by calling LiquidCristal's constructor (a constructor looks like a function and has the same name as the class):

Code:
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

This line instead calls the begin() method on the previously created object "lcd":

Code:
lcd.begin(16, 2);

HTH
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the intention of this line anyway?

Code:
private:
  LiquidCrystal lcd(int, int, int, int, int);   // <---- what is this doing?

Okay, it is actually my bad habit for not commenting the code. smiley-sad
I am using the very basic LCD with 16 columns and 2 rows.
So according to LiquidCrystal.h (int, int, int, int, int) will refers to (rs, enable, d4, d5, d6, d7) respectively.


Well, no it won't. How can it be passing in (rs, enable, d4, d5, d6, d7) when you just have (int, int, int, int, int)?

Quote
I have change the .cpp files a bit to

Code:
(int RS, int ENABLE, int D4, int D5, int D6, int D7, int LCD_COLLUMN, int LCD_ROW):lcd(RS, ENABLE, D4, D5, D6, D7)

The error returned still the same which is "LiquidCrystal does not name a type"

Can you post the amended code please.

Oh okay I am sorry I wrongly look at the tutorial.

http://pastebin.com/GJNYS7TC The .cpp file
http://pastebin.com/ixUfRq90 The .h file

Which is now I get an error "Arduino_Marquee has not been declared"

Quote
The error returned still the same which is "LiquidCrystal does not name a type"

I suspect you still don't get the difference between constructing an object and calling a method on it...

Please have a look at the code here:

http://arduino.cc/en/Tutorial/LiquidCrystal

The following line creates an object named "lcd" by calling LiquidCristal's constructor (a constructor looks like a function and has the same name as the class):

Code:
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

This line instead calls the begin() method on the previously created object "lcd":

Code:
lcd.begin(16, 2);

HTH

Well maybe you correct, I still do not get several term in programming. Well in my opinion LiquidCrystal lcd will somewhat make another object named lcd that can return all function from Liquid.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Well maybe you correct, I still do not get several term in programming. Well in my opinion LiquidCrystal lcd will somewhat make another object named lcd that can return all function from Liquid.

(btw, I didn't mean to be rude) You're right, LiquidCrystal lcd(...) will in fact "make" a LiquidCrystal object, on which you can call LiquidCrystal methods. Bottom line is, just create one lcd object at the beginning of the sketch, and use that through the whole program.

HTH
Logged

Pages: [1]   Go Up
Jump to: