constructors not allowed a return type

This is a discussion on constructors not allowed a return type within the C++ Programming forums, part of the General Programming Boards category; Can anyone see why am I getting the compile time error on this code? It seems pretty silly to me. ...

  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
    22,302
    hmm... did you forget the terminating semi-colon for your class definition?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,048
    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, 05:33 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 11:17 AM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 11:50 AM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 01:34 PM

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