Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > About my own mem_fun1_ref_t.

Reply
Thread Tools

About my own mem_fun1_ref_t.

 
 
Hill
Guest
Posts: n/a
 
      10-23-2008
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))mf(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:rint_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>: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>:perator()(T&, Targ) [with R =
void, T = base, Targ = std::string]' discards qualifiers
 
Reply With Quote
 
 
 
 
Bradley Smith
Guest
Posts: n/a
 
      10-24-2008
Hill wrote:
> 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))mf(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:rint_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>: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>:perator()(T&, Targ) [with R =
> void, T = base, Targ = std::string]' discards qualifiers

 
Reply With Quote
 
 
 
 
Hill
Guest
Posts: n/a
 
      10-25-2008
On 10月24日, 下午4时33分, Bradley Smith <s...@baysmith.com> wrote:
> Hill wrote:
> > 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))mf(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:rint_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>: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>:perator()(T&, Targ) [with R =
> > void, T = base, Targ = std::string]' discards qualifiers- 隐藏被引用文字 -

>
> - 显示引用的文字 -- 隐藏被引用文字 -
>
> - 显示引用的文字 -


Thanks very much
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Allowing access to my own computers within my own network =?Utf-8?B?VHJldm9y?= Wireless Networking 2 07-20-2006 09:05 PM
I have built my own (simple) thread manager [TM], but just found java 5 has its own. Saverio M. Java 0 07-03-2006 08:52 AM
Your own photos in your own book Frank ess Digital Photography 1 12-09-2004 05:54 PM
Using own classloader inside J2EE to load and unload own classes. Stefan Siegl Java 0 09-21-2004 08:11 AM
Spam: Re: Own Your Own On-Line Travel Agency Howard NZ Computing 0 08-01-2003 07:46 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57