-
August 2nd, 2010, 11:27 AM
#1
[RESOLVED] Problems with classes (C2061)
I'll try to be brief. I have several classes in my program, and now I'm having trouble making them work together. The details: I have classes Ball (Ball.cpp + Ball.h) and Wall (Wall.cpp + Wall.h). I was trying to create a new function in Ball with a parameter of type Wall, as follows:
Code:
class Ball
{
[...]
bool CB_col(Wall blck) const;
[...]
};
This is its declaration in Ball.h, line 27. It is also mentioned in Ball.cpp properly.
However, the compiler doesn't seem to accept the code:
Code:
ball.h(27) : error C2061: syntax error : identifier 'Wall'
I was made sure to include the header file:
Ball.cpp
Code:
#ifndef WALL_DECLARE
#define WALL_DECLARE
#include "Wall.h"
#endif
[...]
I've searched through many places about this, but this just feels different from all others. There is absolutely no semi-comma or bracket missing in any of the 4 files.
If there isn't enough info on the problem, it can be provided when requested. Thanks in advance.
-
August 2nd, 2010, 11:40 AM
#2
Re: Problems with classes (C2061)
You should put header guard in the header file. You don't put them around an include statement.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
August 2nd, 2010, 11:58 AM
#3
Re: Problems with classes (C2061)
As you have that define in ball.h you probably wanted to have
Code:
#ifndef BALL_DECLARE
#define BALL_DECLARE
#include "wall.h" // now including could work
...
Regards, Alex
-
August 2nd, 2010, 12:15 PM
#4
Re: Problems with classes (C2061)
Originally Posted by E_net4
I'll try to be brief. I have several classes in my program, and now I'm having trouble making them work together. The details: I have classes Ball (Ball.cpp + Ball.h) and Wall (Wall.cpp + Wall.h). I was trying to create a new function in Ball with a parameter of type Wall, as follows:
Code:
class Ball
{
[...]
bool CB_col(Wall blck) const;
[...]
};
This is its declaration in Ball.h, line 27. It is also mentioned in Ball.cpp properly.
However, the compiler doesn't seem to accept the code:
Code:
ball.h(27) : error C2061: syntax error : identifier 'Wall'
I was made sure to include the header file:
Ball.cpp
Code:
#ifndef WALL_DECLARE
#define WALL_DECLARE
#include "Wall.h"
#endif
[...]
I've searched through many places about this, but this just feels different from all others. There is absolutely no semi-comma or bracket missing in any of the 4 files.
If there isn't enough info on the problem, it can be provided when requested. Thanks in advance.
If you have two classes which work together you better pass argument of the other class by reference rather than by value. Passing by value would make a copy what means in your case that when you pass a wall object to the ball you would get a second wall as a copy in the ball function. That hardly would make sense. I assume you have two walls and one ball, so whenever you pass one of these objects you best pass them by reference.
Code:
int main()
{
Wall left(...);
Wall right(...);
Ball ball;
ball.init(...)
bool missed = false;
while (!missed)
{
missed = ball.fly(left, right);
}
...
}
In the above sample the Ball::fly would have prototype
Code:
bool Ball::fly(Wall & left, Wall & right);
what means that you pass the addresses of existing wall objects rather than passing a copy of wall objects.
Regards, Alex
-
August 2nd, 2010, 12:44 PM
#5
Re: Problems with classes (C2061)
Many thanks for the quick replies. I admit I did the header guards in a bit of a hurry, so I will check into that.
I've understood the idea of passing the parameter by reference, and I agree with that. Guess I was too used to Java.
I performed a complete replacement of the code to accept this declaration:
Code:
bool CB_col(Wall & blck) const;
The main problem (C2061) remains, though.
-
August 2nd, 2010, 12:57 PM
#6
Re: Problems with classes (C2061)
Originally Posted by E_net4
The main problem (C2061) remains, though.
You have to include wall.h in ball.h
// wall.h
Code:
#ifndef WALL_H
#define WALL_H
class Wall
{
//...
};
#endif // WALL_H
// ball.h
Code:
#ifndef BALL_H
#define BALL_H
#include "wall.h"
class Ball
{
//...
void foo(Wall& wall);
};
#endif // BALL_H
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
August 2nd, 2010, 01:11 PM
#7
Re: Problems with classes (C2061)
Alright, that fixed it. The headers were the problem, it seems. Thanks again.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|