About my own mem_fun1_ref_t.

H

Hill

This is just a exercise. I defined my own mem_fun1_ref_t in my_fun
space.
But got compiling error. How to fix it ? Any help is appreciated.

Administrator@HILL /m/tcplex/p3_ch18
$ cat mem_fun1_ref.cpp
#include <list>
#include <functional>
#include <iostream>
#include <algorithm>

namespace my_fun{
//class mem_fun1_ref_t
template<typename R, typename T, typename Targ> class mem_fun1_ref_t:
public std::binary_function<T, Targ, R>
{
R (T::*pmf)(Targ);
public:
explicit mem_fun1_ref_t(R (T::*p)(Targ)):pmf(p){}
R operator()(T& p, Targ arg){return (p.*pmf)(arg);}
};
template<typename R, typename T, typename Targ>
mem_fun1_ref_t<R, T, Targ> mem_fun_ref(R (T::*f)(Targ))
{
return mem_fun1_ref_t<R, T, Targ>(f);
}
}//end namespace my_fun
class base
{
public:
void print_with(const std::string str){
std::cout << "Just for test: " << this << str << std::endl;
}
};
int main()
{
base bn;
std::string str("TEST_STRING");
std::bind2nd(my_fun::mem_fun_ref(&base::print_with),str)(bn);
}
Administrator@HILL /m/tcplex/p3_ch18
$ g++ mem_fun1_ref.cpp
e:/opt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/
3.4.5/bits/stl_function.h: In member function `typename
_Operation::result_type std::binder2nd<_Operation>::eek:perator()
(typename _Operation::first_argument_type&) const [with _Operation =
my_fun::mem_fun1_ref_t<void, base, std::string>]':
mem_fun1_ref.cpp:33: instantiated from here
e:/opt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/
3.4.5/bits/stl_function.h:446: error: passing `const
my_fun::mem_fun1_ref_t<void, base, std::string>' as `this' argument of
`R my_fun::mem_fun1_ref_t<R, T, Targ>::eek:perator()(T&, Targ) [with R =
void, T = base, Targ = std::string]' discards qualifiers
 
B

Bradley Smith

Hill said:
This is just a exercise. I defined my own mem_fun1_ref_t in my_fun
space.
But got compiling error. How to fix it ? Any help is appreciated.

Administrator@HILL /m/tcplex/p3_ch18
$ cat mem_fun1_ref.cpp
#include <list>
#include <functional>
#include <iostream>
#include <algorithm>

namespace my_fun{
//class mem_fun1_ref_t
template<typename R, typename T, typename Targ> class mem_fun1_ref_t:
public std::binary_function<T, Targ, R>
{
R (T::*pmf)(Targ);
public:
explicit mem_fun1_ref_t(R (T::*p)(Targ)):pmf(p){}
R operator()(T& p, Targ arg){return (p.*pmf)(arg);}

Change this to:
R operator()(T& p, Targ arg)const{return (p.*pmf)(arg);}
----------------------------------^^^^^
};
template<typename R, typename T, typename Targ>
mem_fun1_ref_t<R, T, Targ> mem_fun_ref(R (T::*f)(Targ))
{
return mem_fun1_ref_t<R, T, Targ>(f);
}
}//end namespace my_fun
class base
{
public:
void print_with(const std::string str){
std::cout << "Just for test: " << this << str << std::endl;
}
};
int main()
{
base bn;
std::string str("TEST_STRING");
std::bind2nd(my_fun::mem_fun_ref(&base::print_with),str)(bn);
}
Administrator@HILL /m/tcplex/p3_ch18
$ g++ mem_fun1_ref.cpp
e:/opt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/
3.4.5/bits/stl_function.h: In member function `typename
_Operation::result_type std::binder2nd<_Operation>::eek:perator()
(typename _Operation::first_argument_type&) const [with _Operation =
my_fun::mem_fun1_ref_t<void, base, std::string>]':
mem_fun1_ref.cpp:33: instantiated from here
e:/opt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/
3.4.5/bits/stl_function.h:446: error: passing `const
my_fun::mem_fun1_ref_t<void, base, std::string>' as `this' argument of
`R my_fun::mem_fun1_ref_t<R, T, Targ>::eek:perator()(T&, Targ) [with R =
void, T = base, Targ = std::string]' discards qualifiers
 
H

Hill

Hill said:
This is just a exercise. I defined my own mem_fun1_ref_t in my_fun
space.
But got compiling error. How to fix it ? Any help is appreciated.
Administrator@HILL /m/tcplex/p3_ch18
$ cat mem_fun1_ref.cpp
#include <list>
#include <functional>
#include <iostream>
#include <algorithm>
namespace my_fun{
//class mem_fun1_ref_t
template<typename R, typename T, typename Targ> class mem_fun1_ref_t:
public std::binary_function<T, Targ, R>
{
R (T::*pmf)(Targ);
public:
explicit mem_fun1_ref_t(R (T::*p)(Targ)):pmf(p){}
R operator()(T& p, Targ arg){return (p.*pmf)(arg);}

Change this to:
R operator()(T& p, Targ arg)const{return (p.*pmf)(arg);}
----------------------------------^^^^^


};
template<typename R, typename T, typename Targ>
mem_fun1_ref_t<R, T, Targ> mem_fun_ref(R (T::*f)(Targ))
{
return mem_fun1_ref_t<R, T, Targ>(f);
}
}//end namespace my_fun
class base
{
public:
void print_with(const std::string str){
std::cout << "Just for test: " << this << str << std::endl;
}
};
int main()
{
base bn;
std::string str("TEST_STRING");
std::bind2nd(my_fun::mem_fun_ref(&base::print_with),str)(bn);
}
Administrator@HILL /m/tcplex/p3_ch18
$ g++ mem_fun1_ref.cpp
e:/opt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/
3.4.5/bits/stl_function.h: In member function `typename
_Operation::result_type std::binder2nd<_Operation>::eek:perator()
(typename _Operation::first_argument_type&) const [with _Operation =
my_fun::mem_fun1_ref_t<void, base, std::string>]':
mem_fun1_ref.cpp:33: instantiated from here
e:/opt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/
3.4.5/bits/stl_function.h:446: error: passing `const
my_fun::mem_fun1_ref_t<void, base, std::string>' as `this' argument of
`R my_fun::mem_fun1_ref_t<R, T, Targ>::eek:perator()(T&, Targ) [with R =
void, T = base, Targ = std::string]' discards qualifiers- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -

Thanks very much :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,919
Messages
2,570,037
Members
46,444
Latest member
MadeleineH

Latest Threads

Top