Thread: constructors not allowed a return type

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    Question constructors not allowed a return type

    Can anyone see why am I getting the compile time error on this code? It seems pretty silly to me.

    Button.cpp
    Code:
    #include "Button.h"
    
    Button::Button(){
    
    	printf("cenas");
    }
    Error:
    Code:
    1>\carrace\button.cpp(3) : error C2533: 'Button::{ctor}' : constructors not allowed a return type
    In case it's important, the Button.h class is
    Code:
    #pragma once
    
    #include "stdafx.h"
    
    class Button {
    public:
    	Button();
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... did you forget the terminating semi-colon for your class definition?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    lol didn't really know I needed it.

    Thanks a million it did the trick!

    Actually it even took care of 2 strange bugs

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In case you're curious as to why you got that error message . . . after the preprocessor processes your #includes, your source looks like this:
    Code:
    class Button {
    public:
    	Button();
    }
    
    Button::Button(){
    
    	printf("cenas");
    }
    Have you ever seen this syntax before?
    Code:
    struct tag {
        int data;
    } var1, var2;
    It lets you declare instances of a structure at the same time you declare what the structure looks like. (If you do this then you can even leave out the structure tag if you're not going to use it anywhere else.) Well, the same thing applies here. You basically said:
    Code:
    class Button {
    public:
    	Button();
    } Button::Button() {
        std::cout << "cenas" << endl;  // I changed this printf to cout since this is C++!
    }
    In other words, you declared a class Button, and at the same time, you said "use this as the 'type' of the thing that follows". The "thing that follows" happened to be a function definition, so in effect you used the Button class definition as the return type of the constructor. It's pretty much the same thing as
    Code:
    class Button {
    public:
    	Button();
    };
    
    Button Button::Button() {
        std::cout << "cenas" << endl;
    }
    which is why the compiler didn't like it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM