constructor expects string fails but char * is OK!

my code will compile...

ok, as you can see I have a class taking a struct in it's constructor.
the struct ctor expects a string, yet it won't compile if I use one
but a const char * works!

so if I swap the commented line below with the previous I get...

1
2
crap.cpp:26: error: request for member 'speak' in 'bob', which is of non-class type 'thing ()(wotsit)'
gmake: [crap.o] Error 1 (ignored)


It doesn't make sense to me captain.

update:
if I use parenthesis it compiles OK.
thing bob( (wotsit(name)) );
it seems wrong to me.

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
#include <iostream>

using namespace std;

struct  wotsit {
    string moniker;
    explicit wotsit(string s) : moniker(s) {};
};

class thing {

    string name;
    public:
    explicit thing(wotsit s) { name=s.moniker;};
    void speak() { cout <<  "I am " << name << endl; };
};


int main (int argc, char ** argv) {

    string name ("percy");

    thing bob(wotsit("percy"));    // implicit cast to string
    //thing bob( wotsit(name) );  // this line goes wrong but it's correct!!!

    bob.speak();
    return 0;
}
Last edited on
The reason why the first works is because you can construct a string from a const char*.

As for your second error...ouch:
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19
1
2
When the compiler sees bob (wotsit()), it thinks that the wotsit() part is declaring 
a non-member function that returns a wotsit object


aah yes I see now.

stupid compiler.

thanks very much.
;-)
Last edited on
Topic archived. No new replies allowed.