0
Upcoming SlideShare
×

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.
Standard text messaging rates apply

# Template classes and ROS messages

1,158

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

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

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&amp; v1, char&amp; v2) { char temp; temp = v1; v1 = v2; v2 = temp; }
• 4. void swap_values(int&amp; v1, int&amp;v2) { int temp; temp = v1; v1 = v2; v2 = temp; }
• 5. template&lt;class T&gt; void swap_values(T&amp; v1, T&amp; v2) { T temp; temp = v1; v1 = v2; v2 = temp; } Instead, Let&apos;s Use a Template!
• 6. template&lt;class T&gt; void swap_values(T&amp; v1, T&amp; v2) { T temp; temp = v1; v1 = v2; v2 = temp; } (Type) Type Parameter Type Parameter to be replaced
• 7. template&lt;class T&gt; void swap_values(T&amp; v1, T&amp; v2) { T temp; temp = v1; v1 = v2; v2 = temp; } int main() { int integer1 = 1, integer 2 = 2; swap_values(integer1, integer2); cout &lt;&lt; “integer1 now equals ” &lt;&lt; integer1 &lt;&lt; endl; cout &lt;&lt; “integer2 now equals ” &lt;&lt; integer2 &lt;&lt; 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&lt;class T&gt; class Robot { public: void funktion1(T v1, T v2); private: void funktion2(); T v1; T v2; };
• 13. template&lt;class T&gt; class Robot { ... }; template&lt;class T&gt; void Robot&lt;T&gt;::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&lt;class T&gt; 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&lt;char&gt; robot1; Robot&lt;double&gt; robot2; }
• 16. Template Functions in Lab 1 n.advertise&lt;std_msgs::String&gt;(&amp;quot;chatter&amp;quot;, 1000); n.subscribe(&amp;quot;chatter&amp;quot;, 1000, chatterCallback);
• 17. In talker.cpp ros::Publisher chatter_pub = n.advertise&lt;std_msgs::String&gt;(&amp;quot;chatter&amp;quot;, 1000); #1 A Template Function! Scope Resolution Operator
• 18. n.advertise&lt;std_msgs::String&gt;(&amp;quot;chatter&amp;quot;, 1000); Parameter 1 Parameter 2 Scope Resolution Operator
• 19. ros::Subscriber chatter_sub = n.subscribe(&amp;quot;chatter&amp;quot;, 1000, chatterCallback); In listener.cpp Another Template Function! Why no &lt;type&gt;?
• 20. n.subscribe(&amp;quot;chatter&amp;quot;, 1000, chatterCallback); Parameter 1 Parameter 2
• 21. Other Things to Know for Lab1 -The dot operator object.memberfunction -Pointer operations -&gt; and char** object * 0; (*0).dosomething; == 0-&gt;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?