Go Down

Topic: no matching function for call to 'LiquidCrystal::LiquidCrystal()' (Read 7668 times) previous topic - next topic

SSJSuperVegetto

Hello all,

I'm new here and i hope i posted the question in the right topic. I've been looking at the error message for quite a while now and i can't seem to figure it out.

To me it seems the types are correct and all but i'm new to C++ programming so your help would be very much appreciated. I have the following in my class's cpp file:

Code: [Select]
LCDScreen::LCDScreen(int pinRS, int pinE, int pin4, int pin5, int pin6, int pin7) {
pinMode(pinRS, OUTPUT);
pinMode(pinE, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin5, OUTPUT);
pinMode(pin6, OUTPUT);
pinMode(pin7, OUTPUT);
digitalWrite(pinRS, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pin4, LOW);
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
LiquidCrystal LCD_Display(pinRS, pinE, pin4, pin5, pin6, pin7);
}


It gives me an error on the first line which is indicated as the Subject of this Thread.

Thanks in advance for your help.

Kind regards, Bob

PaulS

The instance of LiquidCrystal being defined and initialized in the constructor is NOT the one declared in the class description, which I'm sure is not defined properly, causing the non-existent, no-argument constructor to be invoked.

Read the stickies at the top of the forum. Quote the part that says snippets are OK. Or, do what the threads say.
The art of getting good answers lies in asking good questions.

SSJSuperVegetto

Hello PaulS,

Thank you very much for your reply. I don't fully understand your last sentence but if you mean i should post the whole code then sorry for that i'll try to post the code that is necessary to solve the problem. I read the sticky posts so if i missed something i might have misunderstood (English is not my native language).

You mentioned the instance of LiquidCrystal being defined and initialized in the constructor is NOT the one declared in the class description. I have no idea why not so could you please help me out with this one? I have been searching google for quite some time already but couldn't find anything that helped me along.

The header file:
Code: [Select]

#include "LCDScreen.h"
#include "Arduino.h"
#include "LiquidCrystal.h"

LCDScreen::LCDScreen(int pinRS, int pinE, int pin4, int pin5, int pin6, int pin7) {
_pinRS = pinRS;
_pinE = pinE;
_pin4 = pin4;
_pin5 = pin6;
_pin6 = pin6;
_pin7 = pin7;
init();
}

LCDScreen::~LCDScreen() {

}

void LCDScreen::init() {
// Set Pin Modes
pinMode(_pinRS, OUTPUT);
pinMode(_pinE, OUTPUT);
pinMode(_pin4, OUTPUT);
pinMode(_pin5, OUTPUT);
pinMode(_pin6, OUTPUT);
pinMode(_pin7, OUTPUT);

// Set Output Pins To Low
digitalWrite(_pinRS, LOW);
digitalWrite(_pinE, LOW);
digitalWrite(_pin4, LOW);
digitalWrite(_pin5, LOW);
digitalWrite(_pin6, LOW);
digitalWrite(_pin7, LOW);

// Initialise LiquidCrystal Object
LiquidCrystal LCD_Display(_pinRS, _pinE, _pin4, _pin5, _pin6, _pin7);

LCD_Display.begin(16,2);
LCD_Display.print("TC V1 - Bob");
}


The cpp file:
Code: [Select]

#ifndef LCDSCREEN_H_
#define LCDSCREEN_H_

#include "LiquidCrystal.h"

class LCDScreen {
private:
LiquidCrystal LCD_Display;
int _pinRS;
int _pinE;
int _pin4;
int _pin5;
int _pin6;
int _pin7;
char chrBuffer[16];
void init();

public:
LCDScreen(int pinRS, int pinE, int pin4, int pin5, int pin6, int pin7);
void DisplayMenu(int intMenuItem, double dblValue);
void Update(double dblValue);

virtual ~LCDScreen();
};

#endif /* LCDSCREEN_H_ */


Thank you in advance, Bob

PaulS

Code: [Select]
class LCDScreen {
private:
LiquidCrystal LCD_Display;

This defines a variable called LCD_Display. There are no arguments following the name, so the no argument constructor will be called unless you value this object AT THE SAME TIME YOUR CONSTRUCTOR IS INVOKED.

Code: [Select]

// Initialise LiquidCrystal Object
LiquidCrystal LCD_Display(_pinRS, _pinE, _pin4, _pin5, _pin6, _pin7);

This is a different variable, with the same name as the one defined in the class. It is generally a VERY bad idea to create local variables that mask member variables.

The correct way to value the class member is NOT to create a new instance that immediately goes out of scope. It is to value the class member when the constructor runs:

Code: [Select]
LCDScreen::LCDScreen(int pinRS, int pinE, int pin4, int pin5, int pin6, int pin7) :
  LCD_Display(pinRS, pinE, pin4, pin5, pin6, pin7)
{


And this is why the { goes on a new line!
The art of getting good answers lies in asking good questions.

SSJSuperVegetto

Hello PaulS,

Again thank you for your reply. If i understand correctly the line:
Code: [Select]

// Initialise LiquidCrystal Object
LiquidCrystal LCD_Display(_pinRS, _pinE, _pin4, _pin5, _pin6, _pin7);


Creates a new object of LiquidCrystal? I don't fully understand how you define a new variable and how to define the one in the class, could you please explain a little bit about this?

I have been reading a bit about 'out of scope' but i don't understand why creating a new instance is immediately out of scope, isn't it created within the class or is my understanding of 'out of scope' not correct?

What i did now is the following for the header file:
Code: [Select]

#ifndef LCDSCREEN_H_
#define LCDSCREEN_H_

#include "LiquidCrystal.h"

class LCDScreen {
private:
int _pinRS;
int _pinE;
int _pin4;
int _pin5;
int _pin6;
int _pin7;
char chrBuffer[16];
void init();

public:
LCDScreen(int pinRS, int pinE, int pin4, int pin5, int pin6, int pin7) :
LCD_Display(pinRS, pinE, pin4, pin5, pin6, pin7)
{
_pinRS = pinRS;
_pinE = pinE;
_pin4 = pin4;
_pin5 = pin5;
_pin6 = pin6;
_pin7 = pin7;
LiquidCrystal LCD_Display(pinRS, pinE, pin4, pin5, pin6, pin7);
}
void DisplayMenu(int intMenuItem, double dblValue);
void Update(double dblValue);

virtual ~LCDScreen();
};

#endif /* LCDSCREEN_H_ */


And for the cpp file:
Code: [Select]

#include "LCDScreen.h"
#include "Arduino.h"
#include "LiquidCrystal.h"

LCDScreen::LCDScreen(int pinRS, int pinE, int pin4, int pin5, int pin6, int pin7) {
_pinRS = pinRS;
_pinE = pinE;
_pin4 = pin4;
_pin5 = pin6;
_pin6 = pin6;
_pin7 = pin7;
init();
}

LCDScreen::~LCDScreen() {

}

void LCDScreen::init() {
// Set Pin Modes
pinMode(_pinRS, OUTPUT);
pinMode(_pinE, OUTPUT);
pinMode(_pin4, OUTPUT);
pinMode(_pin5, OUTPUT);
pinMode(_pin6, OUTPUT);
pinMode(_pin7, OUTPUT);

// Set Output Pins To Low
digitalWrite(_pinRS, LOW);
digitalWrite(_pinE, LOW);
digitalWrite(_pin4, LOW);
digitalWrite(_pin5, LOW);
digitalWrite(_pin6, LOW);
digitalWrite(_pin7, LOW);

LCD_Display.begin(16,2);
LCD_Display.print("TC V1 - Bob");
}


I also tried declaring LiquidCrystal LCD_Display again but then i get some errors as well and i'm getting a bit confused right now so your help is very much appreciated.

EDIT: forgot to mention the errors that come up. When i don't declare the variable i get:
Code: [Select]
Multiple markers at this line
- Symbol 'LCD_Display' could not be resolved
- class 'LCDScreen' does not have any field named


with the LiquidCrystal LCD_Display; in the private area i get the following error:
Code: [Select]
'LCDScreen::LCDScreen(int, int, int, int, int, int)' previously defined here
redefinition of 'LCDScreen::LCDScreen(int, int, int, int, int, int)'


Does this mean i should define it but i can remove the constructor in the cpp file? Or am i really completely lost now?


Kind regards, Bob

ieee488

https://www.arduino.cc/en/Hacking/LibraryTutorial excellent tutorial


Is there some reason that you feel the need to create your own class?

https://www.arduino.cc/en/Reference/LiquidCrystal





SSJSuperVegetto

Hello ieee488,

Thank you for the link i looked into it but this doesn't really explain the constructors in detail.


Basically because i want to learn how to do it and also it would make the program easier to read and control i would imagine.


ieee488

I am not sure how it makes it easier.

LiquidCrystal is itself already a class.
You initialize it with the pin numbers.
You write to the LCD with the print() function.
You set the location on the LCD with the setCursor() function.


SSJSuperVegetto

I know, i just want to make things easier for myself by putting some functions in a class and even though it would make it only a little easier, i just want to learn how to make a library which is reason number 1. I've just started with C++ and i want to learn the language (going for a book soon though).


PaulS

Quote
I've just started with C++ and i want to learn the language (going for a book soon though).
Get the book first. When you have read a good portion of the book, the "don't reinvent the wheel" idea should have soaked in. There are plenty of opportunities to build useful classes. There is no reason to build a class that inherits from another class when you don't know what you are doing.
The art of getting good answers lies in asking good questions.

pert

guys I have the same problem in this code attached in the file .can you fix it ?
DON'T CROSS POST!!!!!!!!!!!!!!!!!!!!
http://forum.arduino.cc/index.php?topic=493765
I HAVE REPORTED YOU TO THE MODERATORS

Go Up