Your SlideShare is downloading. ×
0
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Template classes and ROS messages
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Template classes and ROS messages

1,158

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,158
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. -Eric Cote TEMPLATE FUNTIONS/CLASSES IN C++ AND LAB 1: INTRODUCTION TO ROS MESSAGE PASSING
  • 2. Template Functions -What if you need the same function for two different variable types?
  • 3. void swap_values(char& v1, char& v2) { char temp; temp = v1; v1 = v2; v2 = temp; }
  • 4. void swap_values(int& v1, int&v2) { int temp; temp = v1; v1 = v2; v2 = temp; }
  • 5. template<class T> void swap_values(T& v1, T& v2) { T temp; temp = v1; v1 = v2; v2 = temp; } Instead, Let's Use a Template!
  • 6. template<class T> void swap_values(T& v1, T& v2) { T temp; temp = v1; v1 = v2; v2 = temp; } (Type) Type Parameter Type Parameter to be replaced
  • 7. template<class T> void swap_values(T& v1, T& v2) { T temp; temp = v1; v1 = v2; v2 = temp; } int main() { int integer1 = 1, integer 2 = 2; swap_values(integer1, integer2); cout << “integer1 now equals ” << integer1 << endl; cout << “integer2 now equals ” << integer2 << endl; return 0; }
  • 8. Template Classes: A Brief Class Refresher class Robot { public: void funktion1(); private: void funktion2(); int v1; int v2; };
  • 9. class Robot { public: void funktion1(); private: void funktion2(); int v1; int v2; }; Class Name Public Member Function Private Variables and Member Functions
  • 10. class Robot { … } void Robot::funktion2(); { ... } Scope Resolution Operator
  • 11. class Robot { public: void funktion1(); private: void funktion2(); int v1; int v2; }; class Robot { public: void funktion1(); private: void funktion2(); char v1; char v2; }; What if We Want?
  • 12. Time for a Template Class template<class T> class Robot { public: void funktion1(T v1, T v2); private: void funktion2(); T v1; T v2; };
  • 13. template<class T> class Robot { ... }; template<class T> void Robot<T>::funktion1(T v1, T v2); { v1 = v2; } Member Functions Are Also Templates Scope Resolution Operator
  • 14. Do All the Variables in the Class Need to be of Type T? template<class T> class Robot { public: void funktion1(int v1, T v2); private: void funktion2(); int v1; T v2; }; Data Types to be Given by User
  • 15. Using the Template Function int main() { Robot<char> robot1; Robot<double> robot2; }
  • 16. Template Functions in Lab 1 n.advertise<std_msgs::String>("chatter", 1000); n.subscribe("chatter", 1000, chatterCallback);
  • 17. In talker.cpp ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000); #1 A Template Function! Scope Resolution Operator
  • 18. n.advertise<std_msgs::String>("chatter", 1000); Parameter 1 Parameter 2 Scope Resolution Operator
  • 19. ros::Subscriber chatter_sub = n.subscribe("chatter", 1000, chatterCallback); In listener.cpp Another Template Function! Why no <type>?
  • 20. n.subscribe("chatter", 1000, chatterCallback); Parameter 1 Parameter 2
  • 21. Other Things to Know for Lab1 -The dot operator object.memberfunction -Pointer operations -> and char** object * 0; (*0).dosomething; == 0->dosomething; -Unary ++ operator -argc and argv
  • 22. Acknowledgements -Professor Main -Problem Solving with C++ by Walter Savitch—The template function example came from this book. -ROS.org -Sam
  • 23. Questions?

×