Thread: templates using Dev-c++

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    6

    Question templates using Dev-c++

    Hi all

    Do I need to include any specific libraries when working with templates using Dev-c++ ?

    When trying the below I got compiler errors stating that the int type was ambiguous

    #include <iostream>
    #include <string>

    using namespace std;

    template<class T>
    T min(const T &a, const T &b){
    if (a < b)
    return a;
    else
    return b;}

    int main() {

    int a(0),b(2);

    cout << min(a,b);

    return 0;}

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    try taking away the const and see what happens
    Woop?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    min is a standard library template function. Removing const from the parameters will change the function sufficiently to act as an overload and remove ambiguity, but you can have problems with temporary values and it still doesn't address the problem of invading the implementation's namespace. How about, instead of asking for every standard name under the sun with
    Code:
    using namespace std;
    You instead just ask for what you use:
    Code:
    #include <iostream>
    
    using std::cout;
    
    template<class T>
    T min(T &a, T &b){
        if (a < b)
            return a;
        else
            return b;
    }
    
    int main() {
        int a(0),b(2);
    
        cout << min(a,b);
    
        return 0;
    }
    Solving this problem is, after all, the whole purpose of namespaces. Or you could just use std::min:
    Code:
    #include <iostream>
    
    using std::cout;
    using std::min;
    
    int main() {
        int a(0),b(2);
    
        cout << min(a,b);
    
        return 0;
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    6
    Thanx a bunch,

    removing the const worked.

    Ill aslo keep in mind using std::cout.

    Regards
    G

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  2. New to Dev C++/<windows.h>...
    By Cilius in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2005, 01:05 AM
  3. Glut and Dev C++, Programs not Quitting?
    By Zeusbwr in forum Game Programming
    Replies: 13
    Last Post: 11-29-2004, 08:43 PM
  4. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM