Thread: Homework Help

  1. #16
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I do not have a very good understanding of tmplate based functions
    Forget about templates for a momement and write the function three separate times for three separate types, e.g. string, int, and double. What changes do you need to make so that your string function definition works for a list of int's? Make the changes and make sure the code works for a list of int's.

    Then determine what changes you need to make so that your string function works for a list of doubles. By the time you write the 3rd function for doubles, you will realize that all you are doing is copying the exact same function and changing a type here and there. A template function just allows you to specify the type you changed from function to function as a variable so that you don't have to write pretty much the exact same function over and over to handle different types.

    An easy way to template a function is to write the function for one type, like string, then when you get the function to work properly, substitute "T" in for "string" wherever it appears in the function, and then specify "T" as a variable:

    template<typename T>

    it has also been a long time since I had to deal with reference passing. I created the function below the code but cannot seem to be able to get it to call correctly.
    In your string function definition, what type is the function expecting you to send it? Look at what the string function definition specifies between the "( )". Then look at what type you are sending the function:

    maxLoc(max)

    What type is max? Does the type the function is expecting and the type you are sending the function match?
    ----

    Code:
    while (iter!=aList.end())
    Can you identify where a variable named 'iter' was declared?
    ----

    Code:
    if (*iter > *max)
    Can you identify where a variable named 'max' was declared?
    Last edited by 7stud; 12-04-2005 at 03:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM