Pages: [1]   Go Down
Author Topic: question about void, passing data and print\write  (Read 756 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

IDE 1.01
UNO

this is a general question not specific to a program yet so just the void is here

goal. when i print or write some info to a serial port or network port etc the write and print commands take a variity of formats such as a byte, a number, a string and even an array.

so as an example i might have this
 
Code:
lcd.setCursor(10,0);
  lcd.print("ALL in    ");
  lcd.setCursor(5,1);
  lcd.print("Error");

i would like to get it down to something like this

Code:
MyLCD(10,0,"ALL in    ");
MyLCD(5,1,"Error");
void MyLCD(byte col, byte row, ?????? info { \\Here is where i am not sure of the proper datatype for the ?????? so it will take anything just like the print/write does
//do additional stuff here

lcd.setCursor(col,row);
lcd.print(info);
}
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5365
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
MyLCD(10,0,"ALL in    ");
MyLCD(5,1,"Error");
void MyLCD(byte col, byte row, ?????? info { \\Here is where i am not sure of the proper datatype for the ?????? so it will take anything just like the print/write does
//do additional stuff here

lcd.setCursor(col,row);
lcd.print(info);
}


Try this:

Code:
template<class T>
void MyLCD(byte col, byte row, T t)
{
  lcd.setCursor(col,row);
  lcd.print(t);
}
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
MyLCD(10,0,"ALL in    ");
MyLCD(5,1,"Error");
void MyLCD(byte col, byte row, ?????? info { \\Here is where i am not sure of the proper datatype for the ?????? so it will take anything just like the print/write does
//do additional stuff here

lcd.setCursor(col,row);
lcd.print(info);
}


Try this:

Code:
template<class T>
void MyLCD(byte col, byte row, T t)
{
  lcd.setCursor(col,row);
  lcd.print(t);
}

i added that to the code after my void loop
Code:
CMSBridge_ino2:6: error: 'T' has not been declared
CMSBridge_ino2.cpp: In function 'void loop()':
CMSBridge_ino2:246: error: invalid conversion from 'const char*' to 'int'
CMSBridge_ino2:246: error: initializing argument 3 of 'void MyLCD(byte, byte, int)'
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5365
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i added that to the code after my void loop
Code:
CMSBridge_ino2:6: error: 'T' has not been declared
CMSBridge_ino2.cpp: In function 'void loop()':
CMSBridge_ino2:246: error: invalid conversion from 'const char*' to 'int'
CMSBridge_ino2:246: error: initializing argument 3 of 'void MyLCD(byte, byte, int)'

Did you try putting it before the place where it's used?
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i added that to the code after my void loop
Code:
CMSBridge_ino2:6: error: 'T' has not been declared
CMSBridge_ino2.cpp: In function 'void loop()':
CMSBridge_ino2:246: error: invalid conversion from 'const char*' to 'int'
CMSBridge_ino2:246: error: initializing argument 3 of 'void MyLCD(byte, byte, int)'

Did you try putting it before the place where it's used?

no matter where in code i put
Code:
template<class T>
i get the first error that T has not been declared
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Template functions must be defined in a .h file that is then included in the sketch.
Logged

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 46
Posts: 2865
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is the template class available in the IDE and in which lib does it live? Fungus (short for Funny Gustavo?)? Thank You, that Print function would solve a lot of GLCD printing that I need to do with the UTFT Library

Bob
Logged

--> WA7EMS <--
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 144
Posts: 5365
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Arduino IDE is weird about function ordering, it rearranges the code in the sketch before it compiles so that forward references work.

There has to be a way to make it work...you just need somebody who knows more then me about the IDE.

Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 65
Posts: 2112
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Template functions must be defined in a .h file that is then included in the sketch.

No, they can be in the source.
If you include a header, its contents is placed in the spot of the '#include', so its no different to putting it at the top of the sketch. The compiler just needs to see it before its used.

EDIT: PaulS may be right regarding Arduino specifics, it may only copy the function prototype without the previous 'template' line.
Try putting it all on the one line 'template<class T> void MyLCD(byte col, byte row, T t);'  though before separating. Had no trouble with template objects, spose functions are a different kettle of tea.
« Last Edit: November 07, 2012, 06:56:36 pm by pYro_65 » Logged


North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 65
Posts: 2112
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can do 3 different things I tried.

Code:
template<class T>
  struct MyLCDHelper{
    static void MyLCD(byte col, byte row, T t)
      {
     
      }
};

//...
MyLCDHelper< bool >::MyLCD( 2, 1, 0 );

Code:
struct MyLCDHelper{
  template<class T>
    static void MyLCD(byte col, byte row, T t)
      {
       
      }
};

//...
MyLCDHelper::MyLCD( 2, 1, 0 ); //Template auto type deduction.

MyLCDHelper::MyLCD< bool >( 2, 1, 0 ); //Specific template.

Or a not so safe way.

Code:
//This must be the first line of code.
#define TVoid template<class T> void

TVoid MyLCD(byte col, byte row, T t)
      {

      }
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can do 3 different things I tried.

Code:
template<class T>
  struct MyLCDHelper{
    static void MyLCD(byte col, byte row, T t)
      {
     
      }
};

//...
MyLCDHelper< bool >::MyLCD( 2, 1, 0 );

Code:
struct MyLCDHelper{
  template<class T>
    static void MyLCD(byte col, byte row, T t)
      {
       
      }
};

//...
MyLCDHelper::MyLCD( 2, 1, 0 ); //Template auto type deduction.

MyLCDHelper::MyLCD< bool >( 2, 1, 0 ); //Specific template.

Or a not so safe way.

Code:
//This must be the first line of code.
#define TVoid template<class T> void

TVoid MyLCD(byte col, byte row, T t)
      {

      }

i used the not so safe way since it seemed easier, but why is it not so safe??
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 65
Posts: 2112
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Not so much for the actual code, just now there are two things to modify when the return type change. Macros can be iffy, search google for examples.

I didn't think of it before, but just use an anonymous namespace.

Code:
namespace{
  template<class T>
    void MyLCD(byte col, byte row, T t)
      {

      }
}

//...

void setup(){

    MyLCD( 0, 1, 1 );
}

Seems by far the best as the IDE doesn't generate a prototype for it.

« Last Edit: November 07, 2012, 10:23:54 pm by pYro_65 » Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Forget the defines. Using IDE 1.0.2 you can make it work. Example:

Code:
HardwareSerial & lcd = Serial;

// function prototype
template<class T>
void MyLCD(byte col, byte row, T t);

// function definition
template<class T>
void MyLCD(byte col, byte row, T t)
{
//  lcd.setCursor(col,row);
  lcd.print(t);
}

void setup ()  {  } 
void loop ()  {  } 

That compiles without errors.  I simulated the lcd class with a reference to HardwareSerial, but that's not important right now.

The important thing is to prototype the templated function as I did up there. Then the compiler won't generate another prototype in the wrong place.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Forget the defines. Using IDE 1.0.2 you can make it work. Example:

Code:
HardwareSerial & lcd = Serial;

// function prototype
template<class T>
void MyLCD(byte col, byte row, T t);

// function definition
template<class T>
void MyLCD(byte col, byte row, T t)
{
//  lcd.setCursor(col,row);
  lcd.print(t);
}

void setup ()  {  } 
void loop ()  {  } 

That compiles without errors.  I simulated the lcd class with a reference to HardwareSerial, but that's not important right now.

The important thing is to prototype the templated function as I did up there. Then the compiler won't generate another prototype in the wrong place.

this worked to a point, and i may be trying to do more than is possible(easily).

what didnt work is old way
Code:
lcd.print(z[1],HEX);
new way
Code:
MyLCD(0,1,z[1],HEX);
i get
Code:
CMSBridge_ino2.ino: In function 'void loop()':
CMSBridge_ino2:152: error: no matching function for call to 'MyLCD(int, int, byte&, int)'
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12579
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You seem to be passing four parameters to a templated function that expects three.

If you want the function to accept additional arguments such as a format specifier, you need to include those in the declaration and definition.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: