Thread: passing a function as an argument

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    16

    passing a function as an argument

    hi,
    I am trying to write a function that gets an outputstream, a schedule(which is just a list pointers to items), and a function.
    this function should be performed on each item in the schedule, and if it returns true it should print out the item.

    Code:
    std::ostream& printfilteredschedule(std::ostream& os, const Schedule& s, bool condition(const ScheduleItem*))
    {   for (Schedule::const_iterator i = s.begin(); i != s.end(); ++i)
            if (condition(*i))
                (*i)->print(os);
        return os;
    }
    
    // somewhere else in code
    
    bool filteralarm(const FancyScheduleItem* si)
    {   ulint t(time(0));
        return (si->getbegin() >= t && std::min(si->getbegin() - (60 * si->getalarm()), (ulint)0) <= t);
    }
    
    // again somewhere else in the code
    
    else if (showtype == "ALARM")
        {   printfilteredschedule(std::cout, s, filteralarm);
        }
    when I try to compile with this code it give me the following errors:
    58 D:\school\struII\taak3\processcommands.cc [Warning] the address of `bool filteralarm(const FancyScheduleItem*)', will always evaluate as `true'

    [Linker error] undefined reference to `printfilteredschedule(std:stream&, Schedule const&, bool)'

    I hope somebody can explain me what I am doing wrong.

    thanks a lot
    Last edited by angelscars; 12-06-2005 at 08:38 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    in this code condition is supposed to be a function
    Then why did you declare it a bool type:
    bool condition
    You need to pass a pointer to a function as an argument to printfilteredschedule(). Here is an example of the syntax:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int func(double d, string str)
    {
    	cout<<d<<endl;
    	cout<<str<<endl;
    
    	return 10;
    }
    
    void doSomething(double num, int(*funcPointer)(double, string) )
    {
    	cout<<num<<endl;
    
    	funcPointer(12.5, "hello world");
    }
    
    int main() {
    
    	int(*pfunc)(double, string) = func;
    	
    	doSomething(100.5, pfunc);
    	
    	return 0;
    }
    Last edited by 7stud; 12-06-2005 at 08:40 PM.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Why would you need to pass a function as an argument? They're declared globally.

    EDIT: Looking at 7stud's example, I don't quite see why anyone would need to do that. Looks pretty interesting though.
    Last edited by SlyMaelstrom; 12-06-2005 at 08:40 PM.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    ah, I kinda changed my post while you replied.. let me try out your code then

    thanks

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    std::ostream& printfilteredschedule(std::ostream& os, const Schedule& s, bool (*condition)(const ScheduleItem*));
    Be sure to check out http://www.function-pointer.org/

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    thanks for the help (and the link)
    it solved my problem (just to run into another problem a couple of lines later)


  7. #7
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by SlyMaelstrom
    Why would you need to pass a function as an argument? They're declared globally.

    EDIT: Looking at 7stud's example, I don't quite see why anyone would need to do that. Looks pretty interesting though.
    any number of reasons. You might want to write a callback function or you might not want to have a header dependency for a start.

    Look at boost.bind and std::for_each.

    and change your fricking sig! it's damn annoying!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. 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
  3. Passing A Function Into A Constructor
    By fourplay in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2009, 06:06 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Passing my array to function
    By pooty tang in forum C Programming
    Replies: 8
    Last Post: 09-15-2004, 12:19 PM