Function pointer : 'function does not evaluate to a function which takes 2 arguments'

This is a discussion on Function pointer : 'function does not evaluate to a function which takes 2 arguments' within the C++ Programming forums, part of the General Programming Boards category; Hey guys ! I have an error that I can't get rid of. It's 'function does not evaluate to a ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    903

    Function pointer : 'function does not evaluate to a function which takes 2 arguments'

    Hey guys ! I have an error that I can't get rid of. It's 'function does not evaluate to a function which takes 2 arguments'. I have tried modifying cmp_func to (*cmp_func) and (&cmp_func) but neither was the solution.

    Here's the (shortened) code.

    Code:
    class CoursMgr
    {
    public:
    
    
    private:
    	typedef bool (CoursMgr::*CMP_FUNC)(int, int);
    
    	// Gros HACK pas beau..
    	bool FONCTION_NULLE(int, int) { }
    
    };
    Code:
    if(cmp_func == FONCTION_NULLE)
    	if(ComparePPE(trav_it->NoteObtenue, a + b) && ComparePGE(trav_it->NoteObtenue, a - b))
    		ls_resultat.back().Travaux.push_back(*trav_it);
    else 
    	if(cmp_func(trav_it->NoteObtenue, donnee))
    		ls_resultat.back().Travaux.push_back(*trav_it);
    What's weird is that cmp_func can only be a pointer to ComparePPE, ComparePGE and another function, yet the code in blue compiles but not the code in red.
    Any help will be much appreciated.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Where is cmp_func declaration and assignment?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Oh I'm sorry, I think I cut too much code :P

    Code:
    if(type & C_TRAVAIL_NOTE)
    {
    	CMP_FUNC cmp_func = TrouveCmpFunc(str);
    
    	if(cmp_func != 0)
    	{
    		if(cmp_func == FONCTION_NULLE)
    			if(ComparePPE(trav_it->NoteObtenue, a + b) && ComparePGE(trav_it->NoteObtenue, a - b))
    				ls_resultat.back().Travaux.push_back(*trav_it);
    		else 
    			if(cmp_func(trav_it->NoteObtenue, donnee))
    				ls_resultat.back().Travaux.push_back(*trav_it);
    	}
    }
    
    // ...
    
    CoursMgr::CMP_FUNC CoursMgr::TrouveCmpFunc(const std::string& critere)
    {
    	CMP_FUNC func = 0;
    
    	// On regarde si on a ">=x"
    	boost::regex e_pge("^>=\\b[0-9]{1,2}\\b$");
    	if(boost::regex_match(critere, e_pge))
    		func = ComparePGE;
    
    	// On regarde si on a "<=x"
    	boost::regex e_ppe("^<=\\b[0-9]{1,2}\\b$");
    	if(boost::regex_match(critere, e_ppe))
    		func = ComparePPE;
    
    	// On regarde si on a "x"
    	boost::regex e_exact("^\\b[0-9]{1,2}\\b$");
    	if(boost::regex_match(critere, e_exact))
    		func = CompareExact;
    
    	// On regarde si on a "x+-y"
    	boost::regex e_pm("^\\+-\\b[0-9]{1,2}\\b$");
    	if(boost::regex_match(critere, e_pm))
    		func = FONCTION_NULLE;
    
    	return func;
    }
    About the FONCTION_NULLE thing: It's a hack because if TrouveCmpFunction() returns 0, it means that no match was found. However, since the last case of the TrouveCmpFunction() is a special case(i.e. it contains 2 numbers instead of only 1) I cannot assign a function to func, I have to treat this case apart but I cannot return -1 because CMP_FUNC cannot be assigned the value -1 so I made an empty function called FONCTION_NULLE().
    Last edited by Desolation; 01-01-2007 at 11:10 PM.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    member fuctions are actually receive an additional hidden parameter - pointer to this...
    I don't know how to call them by pointer...
    What you can do - make Comparison functions - static members to remove this hidden parameter, and declare the pointer to function as

    typedef bool (*CMP_FUNC)(int, int);
    Then you actualli have a pointer to function receiving 2 parameters and it is working
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    Thank you my friend. Moving my comparison functions and typedef outside of the class (as well as minor adjustments) has solved my problem. Thanks again.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    This has nothing to do with the main topic of this thread but I didn't want to make another thread for such a small issue.

    Okay so I use stringstreams every now and then but in one function, I use it twice.. so I just create a stringstream object and work with it. However, while debugging, I noticed that the dummy test would never produce the expected result.. thus I have decided to go step by step with my debugger and found out that the x variable is changed to the correct value while the 'y' variable is *not* changed at all. I have verified and it seems that 'propre' contains the proper result. I have verified my code many times and concluded that it was the fault of the stringstream.. and it was. I tried to use its flush() and ignore() functions to reset it to a working state, but apparently it didn't work either as the 'y' variable still hadn't changed. However, when I created a second stringstream object and used it for 'y', the variable changed accordingly and I got the expected result. Could you guys enlighten me and tell me how I can modify this really simple piece of code so that it works (i.e. modify 'y') without using a second stringstream object ?

    Code:
    void CoursMgr::ElimineCaracteresPM(int& x, int& y, const std::string& critere)
    {
    	std::string::const_iterator it;
    	for(it = critere.begin(); it != critere.end(); it++)
    		if(*it == '+')
    			break;
    
    	std::string propre;
    	propre.append(critere.begin(), it);
    
    	std::stringstream ss;
    	ss << propre;
    	ss >> x;
    
    	propre.clear();
    	propre.append(it + 2, critere.end());
    
    	ss << propre;
    	ss >> y;
    }
    Many thanks.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    use ss.clear() before entering new data
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    I'll try that ! I don't know why I haven't -.-

    Thanks.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    To clear the value inside the stringstream, use ss.str(""), or in this case ss.str(propre) to reset it to the new value you want to use. Adding ss.clear() is probably wise in case an eofbit or failbit was set with the previous string, but that won't clear the contents of the stream.
    Last edited by Daved; 01-02-2007 at 04:28 PM.

  10. #10
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Desolation
    Thank you my friend. Moving my comparison functions and typedef outside of the class (as well as minor adjustments) has solved my problem. Thanks again.
    you might also want to look into boost.function or boost.bind for all kinds of member-function binding fun!

    see my sig for boost link
    "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. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM

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