Strange Compile Error

Here’s a small fragment of code that causes a weird error:

#include <LiquidCrystal.h>
class obd {
LiquidCrystal lcd(1,2,3,4,5,6);
public:
obd() {}
};

sketch_jun26a:3: error: expected identifier before numeric constant
sketch_jun26a:3: error: expected ‘,’ or ‘…’ before numeric constant

Now, if I move the

LiquidCrystal lcd(1,2,3,4,5,6);

line out of the class definition like this:

#include <LiquidCrystal.h>
LiquidCrystal lcd(1,2,3,4,5,6);
class obd {
public:
obd() {}
};

Then it compiles fine. Does anyone know how to fix this?
Thanks in advance!!

I don't know the source of your problem, but using pin 1 for the LCD is not really a good idea.

I'm no C++ guru, but it seems to me that you are instantiating the LCD inside the class declaration. Shouldn't that be done in the program itself?

OP probably wants access to the LCD thus attempted to do the instantiation inside a class def. If OP would tell more details of the project, maybe we can find a solution.

PaulS: I don't know the source of your problem, but using pin 1 for the LCD is not really a good idea.

I don't think that is a real problem in it's self. Pin 1 is the arduino's Tx pin and thus is a output pin that in his case would be wired to two places, the on-board USB input pin and the external LCD input pin, no problem electrically about that.

It's using pin 0 where a problem exists if you wire an external serial device to it as then you have two output pins, the USB chip and the external device wired together to the arduino Rec pin, and that can only lead to problems in most cases such as failed uploads, etc. The 1k isolation resistor wired in series to the USB pin will prevent electrical damage, but corrupted serial data is sure to happen.

Lefty

@PaulS, The numbers I'm using for the pins are fake. I just couldn't remember which real pins I was using when I started typing this code.

@Bubulindo, Yes, I'm instantiating a LiquidCrystal inside a class definition. This is valid C++ code, and shouldn't cause an error, especially the strange message that I'm getting.

Thanks for you all your feedback so far.

lincomatic:
Here’s a small fragment of code that causes a weird error:

#include <LiquidCrystal.h>

class obd {
  LiquidCrystal lcd(1,2,3,4,5,6);
public:
  obd() {}
};




Does anyone know how to fix this?

I think you have to write:

#include <LiquidCrystal.h>
class obd {
  LiquidCrystal lcd;  // Leave out the constructor arguments
public:
  obd() {}
};

// Define the constructor and explicitly call the constructors for member objects:
obd::obd() : lcd(1,2,3,4,5,6)
{
}

d’oh! I should have known that! Thanks!

lincomatic: This is valid C++ code, and shouldn't cause an error, especially the strange message that I'm getting.

lincomatic: d'oh! I should have known that! Thanks!

Hmmm, guess it isn't so valid after all...

I think you have to write:

Did you try compiling that? The LiquiDCrystal class has 4 constructors, none of which take no arguments.

This compiles, not sure what it does:

#include <LiquidCrystal.h>
#include <Wire.h>

class obd {
  LiquidCrystal lcd;  // Leave out the constructor arguments
public:
  obd()  : lcd(1,2,3,4,5,6) {}
};

obd foo;

void setup () {}

void loop () {}

A variant on John’s code compiles as well:

#include <LiquidCrystal.h>
#include <Wire.h>

class obd {
  LiquidCrystal lcd;  // Leave out the constructor arguments
public:
  obd();
};

// Define the constructor and explicitly call the constructors for member objects:
obd::obd() : lcd(1,2,3,4,5,6)
{
}

obd foo;

void setup () {}

void loop () {}

PaulS: Did you try compiling that? The LiquiDCrystal class has 4 constructors, none of which take no arguments.

Search for: c++ constructor initialization

It can be done like that.

[quote author=Nick Gammon link=topic=65030.msg475334#msg475334 date=1309174244]

PaulS: Did you try compiling that? The LiquiDCrystal class has 4 constructors, none of which take no arguments.

Search for: c++ constructor initialization

It can be done like that. [/quote]

Not only 'can be' but 'has to be', as the original poster found out the hard way. :)