Yesterday I skimmed a large fraction of a C++ book. All through the book they use << for iostreams. As they say, operator overloading is syntactic sugar.
Anyway, last night it occurred to me that it would be interesting to try in LiquidCrystal to add that kind of syntax. But it seems to me that to be worthwhile, I'd also do something for setCursor.
So I thought I'd try to implement this syntactic sugar:
lcd<<"hello expresso machine";
lcd(8,3)<<"bye, now";
This would mean overloading << but tying it to print rather than iostreams
and overloading () and tying it to setCursor.
I thought << would both want to return 'this' so that you could write:
lcd(0,0)<<"upper left"<<" "<<x;
I pared that away as I was trying to get it to compile and changed the type of the fcn to void.
I realize that I will need several overloaded versions of <<, mimicking what is in 'print', but for now one seemed like a place to start. So I have
void LiquidCrystal::operator<< (const char str[]) { //there are a half dozen overloaded versions of print
LiquidCrystal::print(str);
// return this;
}
//void LiquidCrystal:operator() (uint8_t col,uint8_t row) {
void LiquidCrystal:operator() (uint8_t col,uint8_t row) {
setCursor(0,row);
// return this;
}
and the only error left when I compile refers to operator():
/Applications/Arduino.app/Contents/Resources/Java/libraries/LiquidCrystal/LiquidCrystal.cpp:337: error: function definition does not declare parameters
I tried reducing it to one argument (ie just the row) and the message didn't change. I've had little luck trying to google up info on overloading () and the book I have barely mentions that operator. The one instance of overloading () I found looks pretty similar to what I have. It is at:
http://www.parashift.com/c++-faq-lite/operator-overloading.html
the error msg says definition not declaration, but for completeness the .h file has this:
void operator() (uint8_t, uint8_t);
void operator<< (const char*);