compiler error: unexpected constructor, destructor, or type conversion...

This is a discussion on compiler error: unexpected constructor, destructor, or type conversion... within the C++ Programming forums, part of the General Programming Boards category; I've been working on writing a program for school literally all day and I'm getting one error when I try ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    6

    compiler error: unexpected constructor, destructor, or type conversion...

    I've been working on writing a program for school literally all day and I'm getting one error when I try to compile it that I can't figure out. The error I'm getting is "error: expected constructor, destructor, or type conversion before '*' token" and it points me to the top line of this code:

    Code:
    Thing * ClassName::getThing( const OtherThing & ot )
    {
    ...
    }
    anyone know what this means? thank you in advance.

    also, as a quick side-question, in that very function I want to return null. Can I just write "return 0" or should I write "return NULL"? Is there a difference? Or, should I make a variable that's a pointer to a Thing and then just set it to zero and return that?

    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    My guess is its impossible to tell you what the exact problem is. Post your complete code so we have all of the context. Note that a compiler error that points to some line (such as the line(s) you've focused on) may actually have nothing to do with the error. This is often the case when there's a missing ";", missing/extra brackets, etc.

    As for your second question, Cprogramming.com FAQ > NULL, 0, \0 and nul?.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    242
    I'm pretty sure you can use either 0 or NULL for returning the NULL pointer. I prefer NULL for clarity.

    I honestly am not seeing anything fundamentally wrong with the first line of your code. I would guess it has to be a contextual problem--like the stupid one of the missing ';' or having all of your classes in place when this function gets defined.

    I'm not entirely seeing how you'd be getting an unexpected constructor or destructor before the '*' --i.e., of Thing --regardless of context (although strange things can happen with missing ';'). I'd consider why an unexpected type conversion might be taking place if you don't find anything obvious when looking at it again.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Also, the original poster titled the thread as
    error: unexpected constructor...
    but says the error message is
    error: expected constructor...
    I imagine he/she typed the title (incorrectly), and copy/pasted the error message in the post (correctly). In either case, as I said above, its probably a context issue.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    The cause of the error is likely immediately before the function you've provided, as hinted to by the error itself:
    "error: expected constructor, destructor, or type conversion before '*' token"
    The * being the asterisk immediately following Thing:
    Code:
    Thing * ClassName::getThing( const OtherThing & ot )
    The line immediately preceding that is likely the cause.
    Last edited by rags_to_riches; 03-06-2010 at 10:44 PM. Reason: Emphasis in quote

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    6
    oh yea my bad, it's supposed to be "expected" not "unexpected." Thanks for the replies, I don't want to post the whole code just because it's a school assignment.

    Aisthesis - what do you mean by this? (I'm very new to C++)
    Quote Originally Posted by Aisthesis View Post
    ...or having all of your classes in place when this function gets defined.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,662
    Quote Originally Posted by rags_to_riches View Post
    The * being the asterisk immediately following Thing:
    Code:
    Thing * ClassName::getThing( const OtherThing & ot )
    The line immediately preceding that is likely the cause.
    I suspect rags_to_riches has actually pointed correctly at the problem. One specific possibility is a missing semicolon immediately before the line where the error is reported.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    6
    Just through trial and error I finally got it to work, but it's not satisfying since I don't really understand why I needed to change what I did. I had to change it from this
    Code:
    Thing * ClassName::getThing( const OtherThing & ot )
    {
    ...
    }
    to this:
    Code:
    ClassName::Thing * ClassName::getThing( const OtherThing & ot )
    {
    ...
    }
    I probably should've mentioned this before, but Thing is a struct that was defined under "private:" in the header file for the class, ClassName.h (the code above is in ClassName.cpp). Also, not sure if this has anything to do with anything, but the prototype for the function in the code above was also declared privately in the header file. Can anyone please explain to me why I would need to make the change I did?
    Last edited by Ace Rockolla; 03-07-2010 at 08:41 AM.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Basically you get such an error if the compiler doesn't know about the type in question (Thing in this case). If it is really a nested struct, then there is no global type called Thing. Outside of the class you'd have to refer to nested types as you did.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    242
    "Aisthesis - what do you mean by this? (I'm very new to C++)

    Originally Posted by Aisthesis
    ...or having all of your classes in place when this function gets defined.
    that the Thing object (I was wrongly assuming instance of a class, but instance of a structure will also do) is well-defined when you build this function.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,662
    Quote Originally Posted by Aisthesis View Post
    that the Thing object (I was wrongly assuming instance of a class, but instance of a structure will also do) is well-defined when you build this function.
    Look at it from the point of view of a C++ compiler. In your code, it encounters the token "Thing" first, then a "*". With each token, it needs to have information about what that token means. At this point, the compiler is required to know what a Thing is before it reads on (i.e. before it realises it is working on a function named ClassName::getThing()).

    While, yes, it is technically possible for a compiler to read ahead and deduce what Thing is, a C++ compiler is not required to do that - and, in fact, is required not to, except in a few specialised circumstances.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. C Compiler and stuff
    By pal1ndr0me in forum C Programming
    Replies: 10
    Last Post: 07-21-2006, 12:07 PM
  3. Converting Double to Float
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2006, 03:46 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 11:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21