Note that there are some explanatory texts on larger screens.

plurals
  1. PODeclaring and defining a function object inside a class member function
    primarykey
    data
    text
    <p>I wonder if and how it is possible to define a function object inside a classes member function to use it directly with, for example, the std::transform function.<br> I know the example is a bit stupid, it's just to show the problem I'm confronted with. </p> <p>File "example.h"</p> <pre><code>class Example { public: //.. constructor and destructor stuff std::string toString() const; //Converts 'mVal' to a std::string private: std::vector&lt;int&gt; mVal; //Only one digit numbers are allowed ([0-9]) } </code></pre> <p>File "example.cpp"</p> <pre><code>std::string Example::toString() const { //The functor which should be used in std::transform struct { char operator()(const int number) { char c; //"Convert" 'number' to a char return c; }; } functor; //Transform the integers to char std::string str(mVal.size(), '0'); //Allocate enough space std::transform(mVal.begin(), mVal.end(), str.begin(), functor); return str; };//toString() </code></pre> <p>Ever since I tried to implement a function object directly inside a member function like in "example.cpp", the code doesn't get compiled. The error message I get is:</p> <pre><code>error: no matching function for call to ‘transform(__gnu_cxx::__normal_iterator&lt;const int*, std::vector&lt;int, std::allocator&lt;int&gt; &gt; &gt;, __gnu_cxx::__normal_iterator&lt;const int*, std::vector&lt;int, std::allocator&lt;int&gt; &gt; &gt;, __gnu_cxx::__normal_iterator&lt;char*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, Example::toString() const::&lt;anonymous struct&gt;&amp;)’ </code></pre> <p>So I think the problem comes up when using the struct "functor" in std::transform. Can someone tell me what the problem is?</p> <p>Using:<br> gcc-4.2 compiler under Ubuntu Linux.</p> <p>Thanks in advance,<br> René.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload