Thread: Template usage

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    122

    Template usage

    I have been given this template:

    Code:
    template <typename T>
    int addInOrder(T* array, int& size, T value)
    {
      // Make room for the insertion
      int toBeMoved = size - 1;
      while (toBeMoved >= 0 && value < array[toBeMoved]) {
        array[toBeMoved+1] = array[toBeMoved];
        --toBeMoved;
      }
      // Insert the new value
      array[toBeMoved+1] = value;
      ++size;
      return toBeMoved+1;
    }
    This template is in a header file which is included in my cpp file.

    Now, apparently I am not using it correctly in a function I have created in the cpp file.

    This is my usage:

    Code:
    addInOrder(locators, MaxPages, located);
    where locators is the array, MaxPages is the array length, and located is what needs to be inserted.

    Something is wrong because it isn't compiling. Any help would be appreciated. Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If it isn't compiling, it will tell you why. I don't know why you won't tell us.

    But my Hat of Guessing guesses that either you need addInOrder<typename_goes_here>(stuff) or that locators is not an array of the same type as located.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    The compiler is saying:

    Code:
    error: no matching function for call to `addInOrder(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const int&, std::string&)'|

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The error message suggests that locators is not an array of strings. How is it declared?

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    locators is definitely an array of strings.

    Here is the declaration:

    Code:
    string locators[MaxPages];
    Where MaxPages is a const int declared in the function.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Actually I missed the important bit:
    Code:
    error: no matching function for call to `addInOrder(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const int&, std::string&)'|
    Apparently you need to use a non-constant here, or perhaps a const_cast.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Well I need to use the const int..is there any way around this?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Todd88 View Post
    Well I need to use the const int..is there any way around this?
    Change your function so that you don't do ++size, and make the second argument const? There's a reason that second argument can't be const right now.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Incrementing size is kind of the very point of this function. It expects a variable that shows how many items have been added to the array, not how many can be added tops. (I assume you'd check whether you have room to add items before calling this function.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Todd88 View Post
    Well I need to use the const int..is there any way around this?
    Why would you want to use a const int when you will change its value with the function?

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Like C_nuta said, the whole point of const is to protect data either going into or leaving a function. When a peice of data is in this "read-only" state, it CANNOT be changed by any means. The data can be called, and tested against, but no other code or function can modify the assigned value.

    When you write your function prototypes, decide first what data you want protected from function modification. If there are some, delcare them const. I usually have all my constant data in capital letters so I know they must handled differently.

    Code:
    const int MAX_ROWS = 10;
    to

    Code:
    int max_rows = 4;
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Template usage
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-15-2005, 07:07 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM