CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2010
    Posts
    3

    [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.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    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

  3. #3
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Problems with classes (C2061)

    #ifndef WALL_DECLARE
    ...
    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

  4. #4
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Problems with classes (C2061)

    Quote Originally Posted by E_net4 View Post
    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

  5. #5
    Join Date
    Aug 2010
    Posts
    3

    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.

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Problems with classes (C2061)

    Quote Originally Posted by E_net4 View Post
    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

  7. #7
    Join Date
    Aug 2010
    Posts
    3

    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
  •  





Click Here to Expand Forum to Full Width

Featured