Thread: compiler choking on mem_fun_ref()

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    81

    compiler choking on mem_fun_ref()

    Hello, can someone please explain the error I am getting in order to help me get my code to compile?


    Code:
    class RomanNumeral
    {
    	UINT mValue;
    	vector<Symbol> mSymbols;
     
    public:
    	RomanNumeral(const string& romanNumeral);
    
    	void addToSymbols(const char& c)
    	{
    		mSymbols.push_back(Symbol(c));
    	}
    };
    
    RomanNumeral::RomanNumeral(const string& romanNumeral)
    {
    	for_each(romanNumeral.begin(), romanNumeral.end(), mem_fun_ref(&RomanNumeral::addToSymbols));
    	sum fo = for_each(mSymbols.begin(), mSymbols.end(), sum());  // you also must check for subtraction symbols
    	mValue = fo.Total;
    }
    The error my compiler (GCC 4.2.3) is giving:

    /usr/include/c++/4.2/bits/stl_algo.h: In function ‘_Function std::for_each(_InputIterator, _InputIterator, _Function) [with _InputIterator = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Function = std::mem_fun1_ref_t<void, RomanNumeral, const char&>]’:
    problemA.cpp:84: instantiated from here
    /usr/include/c++/4.2/bits/stl_algo.h:159: error: no match for call to ‘(std::mem_fun1_ref_t<void, RomanNumeral, const char&>) (const char&)’
    /usr/include/c++/4.2/bits/stl_function.h:687: note: candidates are: _Ret std::mem_fun1_ref_t<_Ret, _Tp, _Arg>:perator()(_Tp&, _Arg) const [with _Ret = void, _Tp = RomanNumeral, _Arg = const char&]

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You are telling it to use a member function, but you never tell it which object you want to call it with.

    Something like this might work:

    Code:
    for_each(romanNumeral.begin(), romanNumeral.end(), bind1st(mem_fun(&RomanNumeral::addToSymbols), this));
    I also had to change the signature of the function to:
    Code:
    void addToSymbols(char c);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Compiler Paths...
    By Cobra in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2006, 04:04 AM
  3. C Compiler and stuff
    By pal1ndr0me in forum C Programming
    Replies: 10
    Last Post: 07-21-2006, 11:07 AM
  4. I can't get this new compiler to work.
    By Loduwijk in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2006, 06:42 AM
  5. how to call a compiler?
    By castlelight in forum C Programming
    Replies: 3
    Last Post: 11-22-2005, 11:28 AM