Creating Function Templates

This is a discussion on Creating Function Templates within the C++ Programming forums, part of the General Programming Boards category; Hi...I've posted here before and received some help on a program, and now I'm here to get some help on ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    56

    Creating Function Templates

    Hi...I've posted here before and received some help on a program, and now I'm here to get some help on another problem: I'm having trouble understanding the mechanics of function templates and function overloading. I've tried several different ways to write this out. This time I have added my code files to make commenting easier. The point to this program is that it is my first experience with using a randomizer, and it's supposed to create certain 'wanderers' who randomly take a step on a square of a certain size. When a wanderer falls off, he is destructed. I am to overload a function to accept and display two parameters, the first one a string and the second several different data types. I think that once I have the template concept down, I can figure out the overloading from there. Anyway, without further preamble, here are my files, and thank you in advance.
    Attached Files Attached Files

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Basics for overloaded:
    Code:
    void foo(int, int);
    void foo(string, int);
    The two functions have the same name, but take different arguments. The compiler will call the right function depending on the arguments you pass.

    Basics for templates:
    Code:
    template<typename T>
    class myclass
    {
    // Implenetation
    T MyData;
    };
    
    template<typename T>
    void myfunc(T foo);
    T is the data type which is determined when you create the class (or call the function):

    Code:
    int main()
    {
    myclass<int> myint;
    myclass<string> mystring;
    myfunc(5);
    myfunc("test");
    }
    Be sure to read the tutorials on the site.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    Thank you for the quick reply. I am off to class now, but I will come back to you in about 3 hours to be sure I understand. Thanks also for the reminder to check the tutorials.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    Working on the overloading, the identifiers I am using are not found. My function prototype is declared within the class, with empty parameters:
    Code:
    void showStuff();
    then my definitions indicate parameters to be expected, with no class affiliation:
    Code:
    void CWanderer::showStuff(string s, int x)
    {	
    	cout << s << x << endl;
    }
    
    void CWanderer::showStuff(string t, bool y)
    {
    	cout << t << y << endl;
    }
    when calling this, I wrote:
    Code:
    	joe.showStuff("Text ", wandererCount());
    	joe.showStuff("More text ", onSquare());
    So, basically, keeping everything within the class gives me the error messages that the overloaded function is not found in CWanderer and that wandererCount() and onSquare are undeclared identifiers. Making this a non-member function returns the error that wandererCount() and onSquare are undeclared identifiers. Is the problem in the way I am defining/calling the function or in the way that I am declaring the variables?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Declarations should look exactly like your definitions, even if they're overloaded. Declarations are used to tell the compiler that the functions exists! No shortcuts here! Especially with classes, they're part of the blueprint from which the compiler creates an object.
    Redeclare your declarations to match your definitions.

    Code:
    	joe.showStuff("Text ", joe.wandererCount());
    	joe.showStuff("More text ", joe.onSquare());

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,170
    My function prototype is declared within the class, with empty parameters:
    The problem is here. You have a member function named showStuff that takes no arguments. Where in the world do these two other member functions named showStuff come from? Declare them in the class as well.

    Incidentally, it is probably a bad idea to make two function differ only in that one takes an int where the other takes a bool. Certain type conversions could make things ambiguous, if I remember correctly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Quote Originally Posted by laserlight View Post
    Incidentally, it is probably a bad idea to make two function differ only in that one takes an int where the other takes a bool. Certain type conversions could make things ambiguous, if I remember correctly.
    I have a feeling it will do so too. Bool and ints are interchangable, so passing an integer can easily be turned into a bool (!= 0 == true, 0 == false AFAIK).

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    I think I understand. I was thinking that a convenient aspect of overloaded functions means that it is declared ONCE, with an empty parameter list
    Code:
    void showStuff()
    and that the definition file will tell the compiler what all goes in there.
    Replacing that with
    Code:
    void showStuff(string, int);
    void showStuff(string, bool);
    gave me correct output.
    Thank you. Now I will work on templates.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    I agree with laserlight on the convenience and practicality, but the purpose of this particular program is to turn in something indicating that I understand how overloaded functions work. Any data type, as long as I'm returning more than one, would be acceptable.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    I also disagree with your declarations. Some compilers' (like VC) IntelliSense parses the information from your declarations into showing you what the function you try to call looks like. Besides that, it also makes your declarations easier to read than just some function name. By naming the variables, the one who reads your code can deduce what to pass to the function.
    So name your declarations with names on your variables, and not just type. Make them look exactly like your definitions.
    Btw,
    to make the functions more distinguishable, you could make functions such as:
    Code:
    void showStuff(string s, int i);
    void showStuff(string s, double d);
    The compiler determines which function to call depending on the parameters you pass to the function, so a bool and int may not be the best, since a bool IS basically an int.
    Last edited by Elysia; 12-03-2007 at 12:19 PM.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    I agree with that.
    My declaration now looks like this:
    Code:
    void showStuff(string s, int i);
    void showStuff(string t, double d);
    and my definition:
    Code:
    void CWanderer::showStuff(string s, int i)
    {	
    
    	cout << s << i << endl;
    }
    
    void CWanderer::showStuff(string t, double d)
    {
    	cout << t << d << endl;
    }
    finally, I call it the same as before, only passing a literal double value directly into the second parameter.

    I think I almost learn more directly from these forums than I do in class.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,170
    To avoid unnecessary copying, the std::string objects should be passed by (const) reference. Also, since those member functions do not modify the observable state of the CWanderer object, they should be declared as const:
    Code:
    void showStuff(const std::string& s, int i) const;
    void showStuff(const std::string& t, double d) const;
    Code:
    void CWanderer::showStuff(const string& s, int i) const
    {
    	cout << s << i << endl;
    }
    
    void CWanderer::showStuff(const string& t, double d) const
    {
    	cout << t << d << endl;
    }
    Of course, since they do not actually involve CWanderer (they operately entirely based on the arguments passed), they should be non-member functions, by right.

    Another thing: using declarations and directives should almost never be in header files. As such, remove the "using namespace std;" directive from CWanderer.h and then fully qualify names from the std namespace, like how I have edited my example in this post.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    This may not be required in this little example, but in general, do consider naming your variables appropriately. It makes it easier to read and you don't go "huh???" later if you've forgotten what the variables are for.
    You might also place a comment to what they're for.

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    What is a directive?

  15. #15
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    NVM - brain fart.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 06:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21