-1

I wanted to create a function for a class method from a specific instance. As in the example, I'd like to create a function for this->x.

class A {
 public:
  void x(int p) { }
  void y() {
    function<void(int)> f = std::tr1::bind(
      &A::x,
      this,
      std::tr1::placeholders::_1);
  }
};

When I tried to compile this, I got very long error messages. One of them that might make some sense is note: no known conversion for argument 1 from ‘int’ to ‘int&’

woodings
  • 7,503
  • 5
  • 34
  • 52
  • 2
    It's all [working fine](http://liveworkspace.org/code/2Rk5kI$3) on GCC 4.7.2. Which compiler are you using? – chris Jan 06 '13 at 07:48
  • 1
    *no known conversion for argument 1 from ‘int’ to ‘int&’*. HAHA, that is hilarious! Something wrong with your compiler (or compilation process)! – Nawaz Jan 06 '13 at 07:51
  • thanks @chris! I'm on gcc-4.7.1-glibc-2.14.1. I'll try it with a newer gcc. – woodings Jan 06 '13 at 07:52
  • try clang compiler, it might work. – ipinak Jan 06 '13 at 08:16
  • @Nawaz that makes sense if it's using reference terminology to display the value category. An `int&` reference can't bind to a pure `int` rvalue. – Potatoswatter Jan 06 '13 at 08:54
  • 1
    @woodings Better yet, use C++11 instead of TR1. Support for TR1 is likely to get *worse* with newer versions of GCC since it has been superceded. To convert this to a TR1 program, pass `-std=c++11` to the compiler and remove all instances of `tr1::` from the source. – Potatoswatter Jan 06 '13 at 08:55
  • @Potatoswatter I removed all 'tr1' from my code and use std::function and std::bind. It compiled! Thank you for your comment! If you'd like to add an answer, I will vote it as the answer to this question. – woodings Jan 06 '13 at 09:10

1 Answers1

1

It's best to migrate from TR1, which is an informal proposal from 2006, to C++11, which incorporated most of TR1 verbatim (meaning that a TR1 program is probably converted C++11 if you just remove the tr1::s).

Although the interfaces are mostly the same, the TR1 implementation is separate. So it's basically frozen in time, and new compiler quirks might cause it to break. Or on other platforms, they might alias TR1 features forward to "native" C++11 meaning that tr1:: classes might have differences from the actual TR1 spec.

On GCC, after removing tr1::, remove tr1/ from the headers and pass -std=c++11 on the command line.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421