 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jan Boehme Guest
|
Posted: Fri Mar 30, 2007 9:00 pm Post subject: Problem mit bind bei Referenz-Argument |
|
|
Hallo,
in nachfolgendem Code soll ueber for_each die Methode von Objekten
aufgerufen werden, die als Argument eine Referenz auf ein Objekt bekommen.
Aendere ich das Argument auf eine Kopie von std::string, geht es wie
gewuenscht, die nachfolgende Version ergibt jedoch folgenden
Kompilierfehler unter gcc4:
25: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/bits/stl_function.h:435:
error: forming reference to reference type ‘std::string&’
Kann mir jemand Rat geben?
Danke, jan.
<code>
#include <list>
#include <iostream>
#include <functional>
#include <string>
class A
{
int a_;
public:
A( const int &_a ) : a_(_a) { }
std::string& foo( std::string &s) {
std::cout << s << ": " << a_ << std::endl;
return s;
}
};
int main()
{
std::list<A> la;
la.push_back(A(1));
la.push_back(A(2));
la.push_back(A(3));
std::string s("content");
std::for_each( la.begin(), la.end(),
std::bind2nd( std::mem_fun_ref(&A::foo), s ) );
}
</code> |
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Sat Mar 31, 2007 2:10 am Post subject: Re: Problem mit bind bei Referenz-Argument |
|
|
Jan Boehme <aiscape (AT) hotmail (DOT) com> writes:
| Quote: | in nachfolgendem Code soll ueber for_each die Methode von Objekten
aufgerufen werden, die als Argument eine Referenz auf ein Objekt
bekommen.
Aendere ich das Argument auf eine Kopie von std::string, geht es wie
gewuenscht, die nachfolgende Version ergibt jedoch folgenden
Kompilierfehler unter gcc4:
25: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.1/../../../../include/c++/4.1.1/bits/stl_function.h:435:
error: forming reference to reference type std::string&
Kann mir jemand Rat geben?
|
Ja.
Verwende boost::bind (siehe http://www.boost.org/). |
|
| Back to top |
|
 |
Jan Boehme Guest
|
Posted: Tue Apr 03, 2007 3:07 pm Post subject: Re: Problem mit bind bei Referenz-Argument |
|
|
Thomas Maeder wrote:
Danke, damit läuft's. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|