Note that there are some explanatory texts on larger screens.

plurals
  1. POImplicit type unknown when passing pointer to function
    primarykey
    data
    text
    <p>I'm looking at some code at the moment which has been ported and is failing to compile. The code has been written in a rather 'C' like way and is passing function pointers in order to set particular mutators on an object. The object being populated is declared as follows:</p> <pre><code>class Person { std::string n_; int a_; public: void name( const std::string&amp; n ) { n_ = n; } std::string name() const { return n_; } void age( const int&amp; a ) { a_ = a; } int age() const { return a_; } }; </code></pre> <p>Fairly standard stuff. Then we have some interesting functions which I've trimmed for brevity:</p> <pre><code>typedef void (Person::FnSetStr)(const std::string&amp; s); typedef void (Person::FnSetInt)(const int&amp; i); void setMem( const std::string&amp; label, Person* person, FnSetStr fn) { // Do some stuff to identify a std::string within a message from the label. // assume that 'val_s' contains the string value of the tag denoted by // the label. (person-&gt;*fn)(val_s); } void setMem( const std::string&amp; label, Person* person, FnSetInt fn) { // Do some stuff to identify an int within a message from the label. // assume that 'val_i' contains the int value of the tag denoted by the // label. (person-&gt;*fn)(val_i); } </code></pre> <p>And then this gets called as follows:</p> <pre><code>Person* person = new Person; setMem("Name", person, Person::name ); // (1) setMem("Age", person, Person::age ); // (2) </code></pre> <p>The idea seems to be to pass a label, an object and the address of an appropriate mutator. The type of the 3rd parameter is being used to get the compiler to select which overload to call and the specific overload then gets a suitable variable ready and calls the function passing it as a parameter to set the value on the object.</p> <p>This workled on an old Solaris compiler. However, when it compiles on GCC, I get failures at points <code>(1)</code> and <code>(2)</code>:</p> <pre><code>error: no matching function for call to 'setMem( const std::string&amp; label, Person* person, &lt;unknown type&gt; )' </code></pre> <p>It looks like the new compiler treats, say, <code>Person::age</code> as a type rather than a pointer to a function and cannot resolve the overload. I am looking at changing the code to use a function object rather than straight pointers to functions.</p> <p>I wanted to know whether there's a way that the calling code can stay like this (i.e. without an explicitly stating the type that the function takes) bearing in mind that I can't change the <code>Person</code> class and would ideally like to keep changes to a minimum.</p>
    singulars
    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.
    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