Thread: unresolve overloaded function

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463

    unresolve overloaded function

    Code:
    find_if( v1.begin( ), v1.end( ),
    		    not1(bind2nd( mem_fun_ref( &string::find) , string("bleh")) ));
    I keep getting compiling error for that statement above because string::find is overloaded and the compiler can't resolve which version to use. Does anyone know how to use it without having to static_cast it?

    Edit:
    I wrote a functor to incapsulate the value being compared to
    Code:
    struct strFind: private std::string
    {
      strFind(std::string in) : std::string(in) {}
      bool operator() (const std::string & input) const
      {
        return !compare(input);
      }
      
    };
    ...
    int main()
    { ...
    pos = find_if( v1.begin( ), v1.end( ),strFind("pearly") );
    }
    wonder what's the limit of this method is.
    Last edited by nimitzhunter; 01-15-2011 at 09:13 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    You don't. As in, you can't. Not without a cast or an intermediate function.

    EDIT:
    It's one way. Deriving from the standard library classes is generally frowned upon though, since they don't have virtual destructors. A better way would to use composition instead as you can do the entire operation via std::strings public interface:

    Code:
    struct strFind
    {
      std::string needle;
      strFind(const std::string& in) : needle(in) {}
      bool operator() (const std::string & input) const
      {
        return needle == input;
      }
      
    };
    ...
    int main()
    { ...
    pos = find_if( v1.begin( ), v1.end( ),strFind("pearly") );
    }
    Last edited by adeyblue; 01-15-2011 at 09:12 PM.

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by adeyblue View Post
    You don't. As in, you can't. Not without a cast or an intermediate function.

    EDIT:
    It's one way. Deriving from the standard library classes is generally frowned upon though, since they don't have virtual destructors. A better way would to use composition instead as you can do the entire operation via std::strings public interface:

    Code:
    struct strFind
    {
      std::string needle;
      strFind(const std::string& in) : needle(in) {}
      bool operator() (const std::string & input) const
      {
        return needle == input;
      }
      
    };
    ...
    int main()
    { ...
    pos = find_if( v1.begin( ), v1.end( ),strFind("pearly") );
    }

    Thanks for the advice adeyblue. You are right about inheriting from the string class. that just makes the problem more complicated than it should be.
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 07:42 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Problem with overloaded function templates
    By New++ in forum C++ Programming
    Replies: 10
    Last Post: 09-05-2005, 04:00 PM