Passing a set to a template function

This is a discussion on Passing a set to a template function within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm writing this template class and one of the template functions you pass a std::set to of a ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    Passing a set to a template function

    Hi all,

    I'm writing this template class and one of the template functions you pass a std::set to of a template type which then returns one of the elements in the set (which is of course a template type). I have wrote it and it compiles fine, however when i cant seem to use the function correctly without the compiler giving an error. Here is what im trying to do.

    This is the template stuff
    Code:
    template<typename myType>
    class MyTemplateClass
    {
     public:
    
    const myType& PickElement (cont std::set<myType>& mySet) const
    {
     // returns one of the elements in the set
    }
    
    };
    Now how do i use the above function? This is what i have currently got:

    Code:
    std::set<MyEnum> enumSet ;
    MyEnum res ;
    
    res = PickElement<MyEnum>(enumSet) ;
    EDIT:
    Doing a full rebuild resulted in a different error:

    The compiler generates the following error:

    Code:
    error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'MyEnum'
    Thanks for any help
    Last edited by cloudy; 10-09-2007 at 02:21 AM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,652
    PickElement is not a templated function. simply call it like:

    Code:
    res = PickElement(enumSet) ;
    Code:
    #include <ip.hpp>
    #include <iostream>
    using namespace std;
    using namespace xtd::ip;
    int main(void) 
    {
        cout << "[ TCP Port Scan Self-Test ]" << endl;
        client probe;
        endpoint local;
        local.address = "127.0.0.1";
        local.protocol = IPPROTO_TCP;
        for(local.port = 0; local.port < (1 << 16); ++local.port)
        {
            if(probe.open(local))
                cout << "Listening: ";
            else
                cout << "No Response: ";
            cout << local.port << endl;
        }    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. template function v.s. template class
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2007, 12:46 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

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