Classes Issue

Recommended Posts

_Sauce_    116
I am working with MVC++ 2005 Express Edition, and am getting the error C2533 when I try to compile the code.
------ Build started: Project: BiggerFish, Configuration: Release Win32 ------
Compiling...
Fish.cpp
.\Fish.cpp(11) : error C2533: 'Fish::{ctor}' : constructors not allowed a return type
Build log was saved at "file://c:\Documents and Settings\Hillam\My Documents\Visual Studio 2005\Projects\BiggerFish\BiggerFish\Release\BuildLog.htm"
BiggerFish - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

When I typed C2533 in the help files, it told me this: int X::X() { } //C2533. X::X() { } //OK. The problem is that I haven't specified a return balue for this function, so why is it telling me I'm not allowed to? Here is my class, Fish.cpp (Fish.h is somewhere else, and DOES exist, so don't tell me that is the problem):
////////////////////////////////////////////////
// Fish()
////////////////////////////////////////////////
Fish::Fish(void)
{
i_Level = 0;
i_Exp = 0;
i_MoveSpeed = 0;
c_MoveDirection;
b_IsBubble = FALSE;
}

////////////////////////////////////////////////
// ~Fish()
////////////////////////////////////////////////
Fish::~Fish(void)
{
}



tuita    240

Share on other sites
Sneftel    1788
My guess: You have a malformed statement right BEFORE the constructor. Like, perhaps near the end of a header file included by the source file which defines the constructor. Like, perhaps you've left the semicolon off the end of a class definition? For instance, the definition of the Fish class? HMMMM?

When confronted with an error message that seems incorrect, always consider the lines before the line causing the error. After all, C/C++ really has no concept of "lines", just of statements. Unterminated things can make a "line" quite long.

Share on other sites
_Sauce_    116
Well, I'm pretty sure it ISN't the header file because it was working fine but I accidentally deleted the Fish.cpp that went with it, and as a result, had to rewrite it. Nevertheless, I'll try posting the Fish.h file as well.

Share on other sites
Sneftel    1788
Post Fish.cpp, while you're at it.

Share on other sites
Boder    938
Also you don't have to declare a "void" parameter list in C++, and actually it is encouraged not to put it.

Share on other sites
_Sauce_    116
Here is the build log:

------ Build started: Project: BiggerFish, Configuration: Release Win32 ------Compiling...Fish.cpp.\Fish.cpp(11) : error C2533: 'Fish::{ctor}' : constructors not allowed a return typeBuild log was saved at "file://c:\Documents and Settings\Hillam\My Documents\Visual Studio 2005\Projects\BiggerFish\BiggerFish\Release\BuildLog.htm"BiggerFish - 1 error(s), 0 warning(s)========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Fish.cpp:

////////////////////////////////////////////////// Fish.cpp////////////////////////////////////////////////#include "Fish.h"////////////////////////////////////////////////// Fish()////////////////////////////////////////////////Fish::Fish(void){	i_Level = 0;	i_Exp = 0;	i_MoveSpeed = 0;	c_MoveDirection;	b_IsBubble = FALSE;}////////////////////////////////////////////////// ~Fish()////////////////////////////////////////////////Fish::~Fish(void){}////////////////////////////////////////////////// SetLevel()////////////////////////////////////////////////void Fish::SetLevel(int Level){	i_Level = Level;}////////////////////////////////////////////////// GetLevel()////////////////////////////////////////////////int Fish::GetLevel(){	return i_Level;}///////////////////////////////////////////////// SetExp()///////////////////////////////////////////////void Fish::SetExp(int Exp){	i_Exp = Exp;}///////////////////////////////////////////////// GetExp()///////////////////////////////////////////////int Fish::GetExp(){	return i_Exp;}///////////////////////////////////////////////// SetMoveSpeed()///////////////////////////////////////////////void Fish::SetMoveSpeed(int MoveSpeed){	i_MoveSpeed = MoveSpeed;}///////////////////////////////////////////////// GetMoveSpeed()///////////////////////////////////////////////int Fish::GetMoveSpeed(){	return i_MoveSpeed;}///////////////////////////////////////////////// SetMoveDirection()///////////////////////////////////////////////void Fish::SetMoveDirection(char MoveDirection){	c_MoveDirection = MoveDirection;}///////////////////////////////////////////////// GetMoveDirection()///////////////////////////////////////////////char Fish::GetMoveDirection(){	return c_MoveDirection;}///////////////////////////////////////////////// SetIsBubble()///////////////////////////////////////////////void Fish::SetIsBubble(bool IsBubble){	b_IsBubble = IsBubble;}///////////////////////////////////////////////// GetIsBubble()///////////////////////////////////////////////bool Fish::GetIsBubble(){	return b_IsBubble;}///////////////////////////////////////////////// SetLocationX()///////////////////////////////////////////////void Fish::SetLocationX(int LocationX){	i_LocationX = LocationX;}///////////////////////////////////////////////// GetLocationX()///////////////////////////////////////////////int Fish::GetLocationX(){	return i_LocationX;}///////////////////////////////////////////////// SetLocationY()///////////////////////////////////////////////void Fish::SetLocationY(int LocationY){	i_LocationY = LocationY;}///////////////////////////////////////////////// GetLocationY()///////////////////////////////////////////////int Fish::GetLocationY(){	return i_LocationY;	}

And Fish.h:

/////////////////////////////////////////////////////////////// Fish.h/////////////////////////////////////////////////////////////#pragma once#include <windows.h>class Fish{	protected:		int i_Level;		int i_Exp;		int i_MoveSpeed;		char c_MoveDirection;		bool b_IsBubble;		int i_LocationX;		int i_LocationY;	public:		Fish(void);		~Fish(void);		void SetLevel(int Level);		int GetLevel();		void SetExp(int Exp);		int GetExp();		void SetMoveSpeed(int MoveSpeed);		int GetMoveSpeed();		void SetMoveDirection(char MoveDirection);		char GetMoveDirection();		void SetIsBubble(bool IsBubble);		bool GetIsBubble();		void SetLocationX(int LocationX);		int GetLocationX();		void SetLocationY(int LocationY);		int GetLocationY();}

Share on other sites
Sneftel    1788
...

Did you even read my first post? You know, the one where I correctly diagnosed the problem before you'd even posted the code, by reading your mind?

Share on other sites
in the header file, you must put ; after the class declaration.

Share on other sites
_Sauce_    116
Quote:
 Original post by Sneftel...Did you even read my first post? You know, the one where I correctly diagnosed the problem before you'd even posted the code, by reading your mind?

Of course I read it! I just went through my code though and couldn't find any errors in the .h file so I posted everything to see if one of you guys could find it. Also, I added that ; after declaring the class (I thought that was illegal!) and guess what? 30 more errors pop up, so I'll have a look at those if it is legal to put the semicolon where you said.

Share on other sites
Aardvajk    13207
It is more than legal - it is mandatory. Reason is that you can go

class x
{
} instatation;

equivalent to

class x
{
};

x instatiation;

I can't comment on your 30 errors, but I would just say that every single one of your fish member functions would be better expressed as inline functions defined inside the fish class declaration since they are all one statement each, require no additional linkage and could be optimised away by any decent compiler.

Share on other sites
_Sauce_    116
Oh, ok, Maybe I was just putting that ; in the wrong place coz I checked my coding book and none of their classes had the ; where I thought he wanted me to put it so I'll try again in a different place. can you show me EXACTLY how i should edit the code to make it work?

Share on other sites
_Sauce_    116
nevermind I fixed it, thankyou EasilyConfused for that. I wasn't looking at the end of file for that... I can't believe I spent over 4 hours just to find it was a single semicolon at the end of the fiel... SO STOOPID!

Share on other sites
Aardvajk    13207
Relax. It is a far more common mistake than you might think.

Share on other sites
Pwnstar56    100
It is fairly easy to forget simicolons. My first real program came out with over 300 errors, after fixing simicolons, it was down to 3.

Share on other sites
PiCkLeD    188
300! I don't believe it.

Share on other sites
_Sauce_    116
I used to get perhaps 20 semicolon related erros when i first began programming but the major problem here is that I haven't worked with classes for over a year as I didn't see the use in them, but now that I've started game programming, it's a much more useful thing to use. Unfortunately though, I now have to relearn classes and me gettting too excited and ahead of myself casued me to forget that single semicolon. Another example of me getting overexcited is today when I added another 4 classes to my app, and not a single one is currently finished because I constantly realise how I would go about doing something for one of theose other classes as I am working on something else, so my code ends up being half finished. In the current state of my program, if you tried to compile you would get 50+ errors...

Share on other sites
Aardvajk    13207
Just my two cents here but I don't think a good way to learn to program is to hammer out lines and lines of code containing errors then try to correct the errors until it compiles.

IMHO, it is better to write a little bit at a time and make sure each little bit (line, statement, whatever) is compiling okay and behaving as expected before moving on to the next bit. Especially when learning new constructs or features.

And treat compiler warnings as messages saying "This compiles okay but probably won't actually behave as you expect".

Paul

Share on other sites
Quote:
 Original post by EasilyConfusedAnd treat compiler warnings as messages saying "This compiles okay but probably won't actually behave as you expect".

Actualy, when learning, i'd recommend to treat all warnings as errors (thats command line switch /WX for Visual C++). At least, i do this all the time.