lcd.print scope declaration issue in .h files

Hello everyone, I am a newbie with the arduino / c++ language.
I’m trying to understand a bit more about working and interacting with multiple files.
The purpose of the code is to run a function in the main sketch form a separate file, the function will print a message to my LCD.
I have tested the code in serial monitor and worked fine, but when I use lcd.print I get an error.
“functions.cpp error: ‘lcd’ was not declared in this scope”

This is my code so far:
“functions.h” code:


class fclass {
      void FUNCTION1();
      void FUNCTION2();
      void FUNCTION3();

extern fclass start;


“functions.cpp” code:
(I have commented out fclass::FUNCTION1 with the lcd.print command.)

#include "Arduino.h"
#include "functions.h"

fclass::fclass(){ }

void fclass::FUNCTION1() { Serial.println("Test 1"); }

//void fclass::FUNCTION1() { lcd.println("Test 1"); }

void fclass::FUNCTION2() { Serial.println("Test 2"); }

void fclass::FUNCTION3() { Serial.println("Test 3"); }

fclass start = fclass();

This is my main code:

#include <LiquidCrystal.h>               // my lcd library.
#include "functions.h"                   // my header file.
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);     // lcd pins config.

void setup() {
  Serial.begin(9600);           // Begin serial.
  lcd.begin(16, 2);             // Begin my lcd 16x2.
    lcd.print("Message: ");     // Default message.

void loop() {
  lcd.setCursor(0,1);           // Print function output on row 1.
  start.FUNCTION1();            // Loop function 1.

I have read, but I did not find any examples used with lcd.print.
Can someone give me some advice on this issue?

I appreciate all help.


Can someone give me some advice on this issue?

Hopefully Bill (bperrybap) will see this and respond to your question. Meanwhile you might want to follow the links in reply #5 of this thread for some information that may give you some insight into the problem.


Thanks for your reply, Don!

I did not find any useful tutorials or examples similar to this problem.

Yes. "lcd" is out of scope. You have defined it in your main module and I assume that you are trying to reference it inside your sub module and your sub module has no idea what "lcd" is since it is outside the visibility (scope) for that module.

While both Serial and lcd defined to be globals. Serial has an external declaration down in HardwareSerial.h (which is down in the core library) that allows all others outside of the HardwareSerial.cpp module to reference it.

If you want to reference your LiquidCrystal object "lcd" in your sub modules like from your fclass, you will either have to pass a pointer to it in your constructor which you then save in a variable in your fclass and use that pointer instead of "lcd" directly, or you will have to declare an external reference to the "lcd" object.

While using a pointer that is saved in your fclass object is better since it won't require the object to have a specific name like "lcd", adding an external declaration to your header file is easier.

i.e. if you add

extern LiquidCrystal lcd;

to your header file that should clear things up. However, this now requires that you always name the LiquidCrystal object "lcd".

--- bill

I thought C programming was supposed to be simple compared to assembly language.


floresta: I thought C programming was supposed to be simple compared to assembly language.


It is. Even in assembler, if you have a separate source module you must declare your external function or data location "extern" in order to create a reference in the assembled object module that the linker can resolve when everything is linked together.

--- bill

@ Bill

Thank you, for your explanation. Now it makes more sense to me why the lcd was out of scope.