My first attempt at a Template

This is a discussion on My first attempt at a Template within the C++ Programming forums, part of the General Programming Boards category; This doesn't work. It makes sense in my head but it doesn't compile. As usual I've probably done something stupid ...

  1. #1
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124

    My first attempt at a Template

    This doesn't work. It makes sense in my head but it doesn't compile. As usual I've probably done something stupid and am unable to see it. Any help is always appreciated.

    Code:
    #include <iostream> 
    using namespace std;
    
    // Prototypes
    
    
    // Templates
    template < class T >
    T min( T value1, T value2 )
    {   
       T min = value1;
    
       if( value1 > value2 )
       {
          min = value2;
          return min;
       }
       else 
          return min;
    }
    
    int main()
    {
       int number, number1, choose;
       char charac, charac1;
       double doub, doub1;
       
       cout << "Number = 1\nDouble = 2\nCharacter = 3";
       cin >> choose;
       
       switch (choose)
       {
          case 1:
                   cout << "Enter a number: ";
                   cin >> number;
                   cout << "\nEnter a number: ";
                   cin >> number1;
       
                   cout << "\n\nSmallest is: " << min( number, number1 );
                   break;
          case 2: 
                   cout << "Enter a double: ";
                   cin >> doub;
                   cout << "\nEnter a double: ";
                   cin >> doub1;
       
                   cout << "\n\nSmallest is: " << min( doub, doub1 );
                   break;
          case 3:
                   cout << "Enter a char: ";
                   cin >> charac;
                   cout << "\nEnter a char: ";
                   cin >> chrac1;
       
                   cout << "\n\nSmallest is: " << min( charac, charac1 );
                   break;
          }
          
       system( "pause" );
       return 0;
    }

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Did your compiler not tell you the error was?
    Code:
    VC++ 6.0
    G:\jDocument\CSCI\231\experiment\cboard.cpp(53) : error C2065: 'chrac1' : 
    undeclared identifier
    
    .....
    case 3:
                   cout << "Enter a char: ";
                   cin >> charac;
                   cout << "\nEnter a char: ";
                   cin >> chrac1;  <-- misspelled 
    
    ...
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Sorry, how stupid of me. Here is the error.

    In function `int main()':
    call of overloaded `min(int&, int&)' is ambiguous
    candidates are: T min(T, T) [with T = int]
    const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = int]

    call of overloaded `min(double&, double&)' is ambiguous
    candidates are: T min(T, T) [with T = double]
    const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = double]

    call of overloaded `min(char&, char&)' is ambiguous
    candidates are: T min(T, T) [with T = char]
    const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = char]

    Execution terminated
    I've corrected the typo, thanks.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    The identifier min() is already used somewhere in the STL. Just rename your function my_min() or something like that, and it ought to compile.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Oh my word! How thick! It works now. Changed it to minimum. Thanks! *goes to work on Towers*

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    How come it ran fine on my vc++ 6.0???
    EDIT: if we don't include the library/header where min() is in, is there still conflict??
    Last edited by alphaoide; 01-15-2004 at 02:49 PM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    161
    > How come it ran fine on my vc++ 6.0???
    MSVC 6.0 should not be used to gauge anything involving expected or defined behavior (especially when the STL is involved).

    >EDIT: if we don't include the library/header where min() is in, is there still conflict??
    Correct. A better solution is to not "pollute the namespace" with lines like 'using namespace std;'

  8. #8
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Yah, I use 'using namespace std;' only when working on practise exercises (lazy). When I make a proper program I specify each individual item that the program uses from the header files.

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, 05:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  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

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