Thread: errors with class constructor

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    errors with class constructor

    I'm experimenting with classes(for the first time) where basically you create a box with a name, you can move it and close/open it, etc., very simple. However, I get an error when compiling, it says that the constructor is not allowed a return type. What exactly is the error?

    Code:
    BOX::BOX() {
    	xpos=0;
    	ypos=0;
    	open=false;
    }
    What is the return type that it's talking about?

  2. #2
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    where is your declaration? You implementation doesn't have a return type it seems...
    Blue

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    whoops, i also forgot to mention that I'm using Microsoft Visual C++ 6, f.y.i.

    here's the declaration:
    Code:
    class BOX
    {
    public:
    	BOX();
    	~BOX();
    	bool open;
    	int xpos,ypos;
    	char name[31];
    	void openbox();
    	void closebox();
    	void push();
    	void pull();
    	void move(long direction);
    }
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412


    on a hunch... try

    open = 0;
    Blue

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    that wasn't the problem, same error. for the hell of it, here's the deconstructor, it's the same damn thing, but it doesn't have any errors:

    Code:
    BOX::~BOX {
    	xpos=0;
    	ypos=0;
    	open=false;
    }
    [EDIT] No ****, just realized the problem. I have to get rid of the () in the constructor.[/EDIT]
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Yeah... I was confused... it compiled fine for me.
    Blue

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    were you compiling with or without the ()?
    maybe it's a setting in my compiler. because now I get another longer error. It says

    C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MyProjects\type_msg\main.cpp(22) : fatal error C1001: INTERNAL COMPILER ERROR
    (compiler file 'msc1.cpp', line 1786)
    Please choose the Technical Support command on the Visual C++
    Help menu, or open the Technical Support help file for more information
    Error executing cl.exe.
    I have MSDN installed and it couldn't load the specific help page because I need disc 2 (don't have it where i am).

    this is most likely an enviornment configuration that's giving me errors?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  8. #8
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    with... but I let the compiler put in the default destructor as you were not allocating any memory.
    Blue

  9. #9
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    what do you mean with the default deconstructor? Would doing what you do with it make the constructor work?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  10. #10
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    this is the code I compiled into an object file...

    Code:
    in file.h
    ++++++++++++++++++++++
    
    class BOX
    {
      public:
    
          BOX();
    
          int xpos, ypos;
          bool open;
    
          // note:  it is a good idea to make your variables private
    };
    
    // i didn't include macro guards... as I was just testing your situation
    ++++++++++++++++++++++
    
    in file.cpp
    ++++++++++++++++++++++
    #include "file.h"
    
    BOX::BOX()
    {
       xpos=0;
       ypos=0;
       open=false;
    }
    +++++++++++++++++++++++
    that compiled fine for me...

    If you do not explicitly define a constructor/destructor, the compiler puts in a generic one for you. I usually don't do a destuctor unless I allocate memory with 'new'... which I 'delete' in the destructor....

    that is pretty much it.... I do not know what is up with your compiler...
    Blue

  11. #11
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    oh well, i'm about to give up for today, I've included the file if you want to take a look. Anyways, what are macro guards?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  12. #12
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Code:
    ...put void in front of all of these...
    
    BOX::closebox() {
    	open=false;
    }
    BOX::openbox() {
    	open=true;
    }
    BOX::push() {
    	xpos++;
    }
    BOX::pull() {
    	xpos--;
    }
    BOX::move(long direction) {
    	if (direction==RIGHT)
    		ypos++;
    	if (direction==LEFT)
    		ypos--;
    
    ...put the parens back in these...
    
    BOX::BOX (){
    	xpos=0;
    	ypos=0;
    	open=false;
    }
    BOX::~BOX (){
    	xpos=0;
    	ypos=0;
    	open=false;
    }
    Blue

  13. #13
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    when all else fails... try simplicity to determine your problem.

    Write a simple class that does nothing and compile it. Find out what your errors are from there, and then see if you can find those errors in your bigger problem.

    Code:
    class test
    {
       public:
           test();
           int a;
    };
    
    test::test()
    {
       a = 0;
    }
    
    int main()
    {
       test it;
       
       return 0;
    }
    think about it
    Last edited by Betazep; 03-17-2002 at 01:39 AM.
    Blue

  14. #14
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    ok, i did what you said, however, if I put the parenthesis back in the deconstructor, i get 19 errors. if i put them in the constructor(and not in the deconstr.) I get an error about how it can't have a return type. I have the voids like:

    Code:
    void BOX::move(long direction) {
    	if (direction==RIGHT)
    		ypos++;
    	if (direction==LEFT)
    		ypos--;
    }
    and it didn't seem to do much. gar! (that's not directed at you.)
    Last edited by neandrake; 03-16-2002 at 08:25 PM.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  15. #15
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    then compile the code I gave above...
    Blue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-29-2008, 04:37 AM
  2. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  3. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 05:24 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Unresolved external on vector inside class constructor
    By Mario F. in forum C++ Programming
    Replies: 13
    Last Post: 06-20-2006, 12:44 PM