class problem

Hi. I have a problemm with classes.
-I have a class called Ball and a class called playerBrick;
-in a definition of a method of class Ball i want to use a parameter of type playerBrick;

and i get this error:
C:\Users\Radu\Desktop\Destop\BrickGraph\ball.h:14: error: 'playerBrick' has not been declared

Classes are in different files but I included 1 in the other so don't think this is the problem;


Main code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <graphics.h>
#include "playerBrick.h"
#include "bricks.h"
#include "ball.h"
#include <cmath>
using namespace std;


int main()
{
    cout<<"Press esc to close the graph!";
    char ch;
    initwindow(1500,700);
    bricks bObj;
    ball ballObj;
    playerBrick bo;
    //bo's pointer
    playerBrick *pBo;
    pBo=&bo;
    ballObj.hit(100,100,700,700);
    while(1)
    {
        ch=getch();
        if((int)ch==97)
            bo.moveLeft();
        else if((int)ch==100)
            bo.moveRight();
        else if((int)ch==27)
        {
            closegraph();
            cout<<"Graph closed!";
        }
    }
    return 0;
}

ball.h code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef BALL_H
#define BALL_H
#include "playerBrick.h"


class ball
{
    public:
        ball();
        bool exist(double x, double y);
        void hit(double x, double y, double p, double q);
        void clean(double x, double y, double r);
        void printCoords();
        void move_playerBrick(playerBrick pBo);
    private:
        double x,y,r;
        double atmX,atmY;
};

#endif // BALL_H 

ball.cpp code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "ball.h"
#include <iostream>
#include <graphics.h>
#include <cmath>
#include "playerBrick.h"

using namespace std;

ball::ball()
{
    setcolor(BLACK);
    x=625;
    y=350;
    r=50;
    circle(x,y,r);
}

bool ball::exist(double x, double y)
{
    if(x>=60&&x<=1440&&y>=60&&y<=640)
        return 1;
    else
        return 0;
}

void ball::hit(double x, double y, double p, double q)
{
    double biggerRatio;
    double r1,r2;
    r1=(p-x)/10;
    r2=(q-y)/10;
    if(abs(r1)>abs(r2))
        biggerRatio=r1;
    else
        biggerRatio=r2;
    r1=(p-x)/biggerRatio;
    r2=(q-y)/biggerRatio;
    double i,j;
    for(i=x, j=y; i<=p-x&&j<=q-y; i+=r1,j+=r2)
    {
        setcolor(GREEN);
        circle(x+i,y+j,r);
        delay(100);
        if(exist(x+i,y+i)==0)
            break;
        setcolor(BLACK);
        circle(x+i,y+j,r);
    }
    cout<<x+i<<" "<<y+i;
}

void ball::clean(double x, double y, double r)
{
    setcolor(BLACK);
    circle(x,y,r);
}

void ball::printCoords()
{
    cout<<"x="<<x<<endl<<"y="<<y;
}

void ball::move_playerBrick(playerBrick pBo)
{
    {
    char ch;
    while(1)
    {
        ch=getch();
        if((int)ch==97)
            bo.moveLeft();
        else
            if((int)ch==100)
                bo.moveRight();
            else
                if((int)ch==27)
                {
                    closegraph();
                    cout<<"Graph closed!";
                }

    }
}
}

bricks.h code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef BRICKS_H
#define BRICKS_H


class bricks
{
    public:
        bricks();
        //bool exist();
        void clean(double x1, double y1, double x2, double y2);
    private:
        int a[7];
        double x1,y1,x2,y2;

};

#endif // BRICKS_H 

bricks.cpp code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "bricks.h"
#include <iostream>
#include <graphics.h>

bricks::bricks()
{
    setcolor(RED);
    rectangle(20,50,220,150);
    rectangle(230,50,430,150);
    rectangle(440,50,640,150);
    rectangle(650,50,850,150);
    rectangle(860,50,1060,150);
    rectangle(1070,50,1270,150);
    for(int i=1; i<=6; i++)
        a[i]=1;
}

/*bool bricks::exist()
{

}*/

void bricks::clean(double x1, double y1, double x2, double y2)
{
    setcolor(BLACK);
    rectangle(x1,y1,x2,y2);
}


playerBrick code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef PLAYERBRICK_H
#define PLAYERBRICK_H
#include "ball.h"
class playerBrick//: public ball
{
public:
    playerBrick();
    //void execut_move(playerBrick bo);
    void moveLeft();
    void moveRight();
    void clean();
    bool exist(double x1, double y1, double x2, double y2);
private:
    double x1,y1,x2,y2;
};
#endif // PLAYERBRICK_H 

playerBrick.cpp code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "playerBrick.h"
#include <iostream>
#include <graphics.h>

using namespace std;

playerBrick::playerBrick()
{
    setcolor(GREEN);
    rectangle(500,600,1000,700);
    x1=500;
    y1=600;
    x2=1000;
    y2=700;
}

void playerBrick::moveLeft()
{
    if(exist(x1-10,y1,x2-10,y2)==1)
    {
        clean();
        x1-=10;
        x2-=10;
        setcolor(GREEN);
        rectangle(x1,y1,x2,y2);
    }
}

void playerBrick::moveRight()
{
    if(exist(x1+10,y1,x2+10,y2)==1)
    {
        clean();
        x1+=10;
        x2+=10;
        setcolor(GREEN);
        rectangle(x1,y1,x2,y2);
    }
}

void playerBrick::clean()
{
    setcolor(BLACK);
    rectangle(x1,y1,x2,y2);
}

bool playerBrick::exist(double x1, double y1, double x2, double y2)
{
    if(x1>=10&&x2<=1370)
        return 1;
    else
        return 0;
}


I want to make a brick game using graphics.h library;
It is not ready yet i have to find this error and then insert another method in ball class which using hit method to make the natural move;
sorry for my bad english. Can i get help please :)
Last edited on
You have a circular dependency. Read this article to learn how to resolve it:
http://www.cplusplus.com/articles/Gw6AC542/
Thanks LB! I understood and fixed it.

Now i have another problem:

ball.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "ball.h"
#include <iostream>
#include <graphics.h>
#include <cmath>

using namespace std;

ball::ball()
{
    setcolor(BLACK);
    x=625;
    y=350;
    r=50;
    circle(x,y,r);
}

bool ball::exist(double x, double y)
{
    if(x>=60&&x<=1440&&y>=60&&y<=640)
        return 1;
    else
        return 0;
}

void ball::hit(double x, double y, double p, double q, playerBrick pBo)
{
    double biggerRatio;
    double r1,r2;
    r1=(p-x)/10;
    r2=(q-y)/10;
    if(abs(r1)>abs(r2))
        biggerRatio=r1;
    else
        biggerRatio=r2;
    r1=(p-x)/biggerRatio;
    r2=(q-y)/biggerRatio;
    double i,j;
    for(i=x, j=y; i<=p-x&&j<=q-y; i+=r1,j+=r2)
    {
        move_playerBrick(pBo);
        setcolor(GREEN);
        circle(x+i,y+j,r);
        delay(80);
        if(exist(x+i,y+i)==0)
            break;
        setcolor(BLACK);
        circle(x+i,y+j,r);    }
    cout<<x+i<<" "<<y+i;
}

void ball::clean(double x, double y, double r)
{
    setcolor(BLACK);
    circle(x,y,r);
}

void ball::printCoords()
{
    cout<<"x="<<x<<endl<<"y="<<y;
}

void ball::move_playerBrick(playerBrick pBo)
{
    {
    char ch;
        ch=getch();
        if((int)ch==97)
                pBo.moveLeft();
        else
            if((int)ch==100)
                pBo.moveRight();
            else
                if((int)ch==27)
                {
                    closegraph();
                    cout<<"Graph closed!";
                }



}
}




Here is the problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void ball::hit(double x, double y, double p, double q, playerBrick pBo)
{
    double biggerRatio;
    double r1,r2;
    r1=(p-x)/10;
    r2=(q-y)/10;
    if(abs(r1)>abs(r2))
        biggerRatio=r1;
    else
        biggerRatio=r2;
    r1=(p-x)/biggerRatio;
    r2=(q-y)/biggerRatio;
    double i,j;
    for(i=x, j=y; i<=p-x&&j<=q-y; i+=r1,j+=r2)
    {
        move_playerBrick(pBo);
        setcolor(GREEN);
        circle(x+i,y+j,r);
        delay(80);
        if(exist(x+i,y+i)==0)
            break;
        setcolor(BLACK);
        circle(x+i,y+j,r);    }
    cout<<x+i<<" "<<y+i;
}


I want the ball to move all time but it don't because in this for i call move_playerBrick(pBo) which is waiting to assign a character to ch so it stop the execution and ball stacks(well it dissapears because there is setcolor(BLACK) and it don't become again green because of that call. And for some reasons i can move the playerBrick just 1st time i press a/d key and then i should just press smth to refresh the ball moves.
What should I do to be allowed always to move the playerBrick but in same time ball to move without waiting for my assign?

PS: In this stage I want the ball just to able to use this call from main:
ballObj.hit(100,100,700,700, *pBo);// hit(x,y,p,q,*pBo) where x and y are curent coords and //p,q are the target coords
and then I will make another method which using this function will set the coords to shoot and move it naturally.
Last edited on
It is good practice to include header files wherever the declarations they contain are needed.

playerBrick.h includes ball.h, and ball.h includes playerBrick.h. This is what we call a circular dependency. You need to rethink your design.


holy wall of text... I didn't see that LB already answered.
Last edited on
You have discovered why the console is bad for games.
http://www.cplusplus.com/articles/G13hAqkS/

Use a graphics library like SFML or Magnum. The console will only cause you pain and suffering.
Topic archived. No new replies allowed.