Trying to call method/array on different .cpp problem

Basically, I have multiple .cpp files in one program. One .cpp file contains the command

visited.push_back( startSquare ); and i want to use that command in a different .cpp file. The first wfile is ofr course where the array is created, etc.

Can anyone tell me in lamens terms what i need to do in another class if i want to call that array and add to the data?

(i want the command to be using the same array which is visited)
Last edited on
This sounds like a class design problem. Why is the code for dealing with visited split into two different source files?
oh no, the array is working fine and the command is working fine.

The command visited.push_back( startSquare ); is in one class, ill call that class
A.cpp
There is no problems.

I want to use the same command in another class, ill call that class
B.cpp


the problem is i don't know what i have to do to allow me to use this command in a different class as the array doesnt exist in the B.cpp and i want to use the same array on both A.cpp and B.cpp. If that makes sense?
Last edited on
Why does the other class need to deal with or even know about the first class' stuff? Like I said, this sounds like a class design problem.
Last edited on
ah, because B.cpp deals with generating a board while A.cpp handles the AI movement on it.

the way i've done my code, the ai doesnt step on spaces its been on already i.e. visited.

whenever a movement is made the space the AI is on is added to visited using visited.push_back( x,y );

I want to take that code and use it when generating the board to convince the AI that "walls" are in fact spaces it has previously tried to walk on but failed.

Thus adding wall recognition a sneaky way :p
Last edited on
The AI class should do that itself. It is not the responsibility of the board class to inform the AI where it should not walk; the AI should see the board and know where not to walk.
yeh, but yesterday i made the board generate randomly. Other students are making a staple board and thus can keep it all in one class.

The board is basically completely separate to the AI as far as i can tell. At the moment it goes through walls like a ghost. I want to add some code to the generation of the random walls that includes adding the spaces the walls consume as spaces the AI cannot travel on.
How the board is generated is not relevant; the AI should be able to look at the board and detect obstacles. The board should have no idea that the AI even exists.
ah but we are not doing proper AI. The way we have coded it, the "AI" simply moves in a random fashion over and over again, then when it ends up in the right place. It takes that specific path and displays it. But each time if it corners itself, it stops and restarts, that space it stopped on becomes a space it will never try to step on again.

I want to make walls those spaces because the walls are purely visual at the moment and have no relation with the AI.
Last edited on
I know. I'll be direct this time: in the AI's constructor, go through the board and mark the walls as places that were visited.
but the board is randomly generated, the walls are never in the same place. I cant alter the AI constructor because that is in A.cpp/.h and the walls generating code is in B.cpp which is basically my initial problem in reverse. :(
Last edited on
is it even possible to alter an array from a different class, this is what i want to know most?
Computer Hermit wrote:
but the board is randomly generated, the walls are never in the same place.
For the last time already, this does not matter.
Computer Hermit wrote:
is it even possible to alter an array from a different class, this is what i want to know most?
In a properly designed program, no. In a poorly design program, yes.

I think there is some misunderstanding here. Let me lay out what I am think you should be able to have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Board
{
    int[][] grid;
    int w, h;
    const static int WALL = 3;
public:
    Board(int xsize, int ysize) : w(xsize), h(ysize)
    {
        grid = /*x, y*/;
    }
    bool isWall(int x, int y)
    {
        return grid[x][y] == WALL;
    }
    int XSize(){ return w; }
    int YSize(){ return h; }
    //...
};
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
#include "Board.hpp"
class AI
{
    bool[][] visited;
    Board &board;
public:
    AI(Board b) : board(b)
    {
        visited = /*board.XSize(), board.YSize()*/;
        for(/*x, y*/)
        {
            if(board.isWall(x, y))
            {
                visited[x][y] = true;
            }
            else
            {
                visited[x][y] = false;
            }
        }
    }
    void TraverseBoard()
    {
        //...
    }
};
1
2
3
4
5
6
int main()
{
    Board board (10, 10);
    AI ai (board);
    ai.TraverseBoard();
}
(This is all pseudo-code, of course)

Is this not like what you have? Because in theory it should work fine.
ok, sorry for the late reply, Ill have a look at your example now
ok, this looks like what i was thinking. I just wasn't sure how exactly you would go about this. Thanks for the help (despite my ignorance), ill let you know how it goes :D

Last edited on
I ended up just using the line theTour.getVisited().push_back(Square(xValue,yValue)); :p
Topic archived. No new replies allowed.