Won't compile

This is just a syntax problem.

I have three programs here - Unit.h, Unit.cpp, and Game.cpp. Game.cpp includes Unit.h.

The issue is with passing the two-dimensional array board[r][c].

Here's my Game.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
 #include <iostream>
using namespace std;

#include "Unit.h"

static const int r = 15;
static const int c = 15;

char board[r][c];

int main()
{
        void printBoard();

        cout << endl;

        for (int Row = 1; Row < r-1; Row++)
        {
                for (int Col = 1; Col < c-1; Col++)
                {
                        board[Row][Col] = ' ';
                }
        }

        Unit Alexander (52, 80, 1, 10, 3, 20, 1, true, 2, 8);

        board[Alexander.get_ypos()][Alexander.get_xpos()] = 'A';

        printBoard();

        Alexander.Move(board[r][c], 3, 9)

        printBoard();

}



Here's the .h of my Unit class

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
#ifndef Unit_h
#define Unit_h

#include <stdio.h>
#include <stdlib.h>
#include <string>

//#include "Student.h"
//#include "Node.h"

class Unit
{
        public:
                // Default constructor
                Unit();

                // Constructor that specifies a unit
                Unit(int h, int a, int w, int c, int m, int p, int r, bool l, int x, int y);

                // Unit moves across arena, returns whether it's a legal movement. Takes in coordinates as arguments.
                bool Move(char board[], int x, int y);

//....there's also setter and getter functions, don't need to post everything 

 private:

                int health;
                int armor;
                int wait;
                int counter;
                int move;
                int power;
                int range;//?????? will have to be a set of coordinates

                bool living;
                int xpos;
                int ypos;
};
#endif 


And here's my .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
#include "Unit.h"

#include <iostream>
using namespace std;

Unit:: Unit()
{
        health = 0;
        armor = 0;
        wait = 0;
        counter = 0;
        move = 0;
        power = 0;
        range = 0;
        xpos = 0;
        ypos = 0;
        living = false;
}

Unit:: Unit(int h, int a, int w, int c, int m, int p, int r, bool l, int x, int y)
{
        health = h;
        armor = a;
        wait = w;
        counter = c;
        move = m;
        power = p;
        range = r;
        living = l;
        xpos = x;
        ypos = y;
}

bool Unit:: Move(char board[], int x, int y)
{
        bool legalMove = true;

        int x_distance;
        int y_distance;
        int tot_distance;

        if (x == 0 || y == 0 || x > 13 || y > 13)
                legalMove = false;
        else
                if ((x == 1 && y == 1) || (x == 1 && y == 13) ||(x == 13 && y == 1) ||(x == 13 && y == 13))
                        legalMove = false;
                else
                {
                        x_distance = abs(x - this -> get_xpos());
                        y_distance = abs(y - this -> get_ypos());
                        tot_distance = x_distance + y_distance;

                        if (tot_distance > this -> getMove())
                                legalMove = false;
                }
     if (legalMove)
        {
                board[ypos][xpos] = ' ';
                xpos = x;
                ypos = y;
                board[ypos][xpos] = 'A';
        }
        else
                cout << "You can't move there";


        return legalMove;
}

//....and there's also implementation of setter and getter functions 

The move function expects a pointer to an array of char, and 2 integers.

You called this function with a char and 2 integers

E: Also where have you declared printBoard?
Last edited on
Thanks a lot for the help!

And sorry....the printBoard function is defined beneath the main function in the game.cpp.....I neglected to include it in the post, but it's there.

And I understood what you said about the error with the character versus pointer, but still, the syntax eludes me.

I tried making it :

1
2
3
Alexander.Move(board, 3, 9)
Alexander.Move(*board, 3, 9)
Alexander.Move(&board, 3, 9)


None of those got rid of the compiling problems. And I have no idea which one is right.

And, I don't know if this helps or not, but I'll post the full Game.cpp, with the printBoard:


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
#include <iostream>
using namespace std;

#include "Unit.h"

static const int r = 15;
static const int c = 15;

char board[r][c];

int main()
{
	void printBoard();

        cout << endl;

	for (int Row = 1; Row < r-1; Row++)
        {
                for (int Col = 1; Col < c-1; Col++)
                {
			board[Row][Col] = ' ';
                }
        }	

        Unit Alexander (52, 80, 1, 10, 3, 20, 1, true, 2, 8);

        board[Alexander.get_ypos()][Alexander.get_xpos()] = 'A';
	
	printBoard();

	Alexander.Move(board, 3, 9);

	printBoard();
	
}

void printBoard()
{

	//Number the columns so the user can easily identify coordinates
        cout << "    ";
        for (int i = 1; i < r-1; i++)
        {
                cout << i;
		if (i <= 9)
			cout << "  ";
        	else
			cout << " ";
	}

        cout << endl;
        for (int Row = 1; Row < r-1; Row++)
        {
                cout << Row;
		if (Row <= 9)
			cout << "  "; // Output Row so the rows are numbered
                else
			cout << " ";
		for (int Col = 1; Col < c-1; Col++)
                {
                        if ((Col != 1 || Row != 1) && (Col != 13 || Row != 1) && (Col != 1 || Row != 13) && (Col != 13 || Row != 13) )
				cout << "[" << board[Row][Col] << "]";
			else
				cout << "   ";
			
                }
                cout << "\n";
        }

        cout << endl;
}


Last edited on
The second one should have gotten rid of the compiler issues. What error do you get after using that?
Yeah, I thought the second one would be the right one.

Here's the error:

Unit.cpp: In member function âbool Unit::Move(char*, int, int)â:
Unit.cpp:63:33: error: invalid types âchar[int]â for array subscript
Unit.cpp:66:33: error: invalid types âchar[int]â for array subscript

Referring to these lines:

1
2
3
4
5
6
7
if (legalMove)
        {
                board[ypos][xpos] = ' '; //ERROR
                xpos = x;
                ypos = y;
                board[ypos][xpos] = 'A'; //ERROR
        }

I was playing around with it and I noticed something interesting.

This compiles:

1
2
3
4
5
6
7
if (legalMove)
        {
                board[ypos] = ' '; 
                xpos = x;
                ypos = y;
                board[ypos] = 'A'; 
        }


Which means the computer thinks it's a single dimensional array??
Well this is different. Those lines do not work because the variable board refers to a pointer to char array. If you want those lines to work, the function:

bool Unit:: Move(char board[], int x, int y)

Should instead be:

bool Unit:: Move(char *board[], int x, int y)
or
bool Unit:: Move(char board[][], int x, int y)
or
bool Unit:: Move(char **board, int x, int y)

Now in main, you should call the function the way you called it previously and that should get rid of the errors

Read about passing arrays to functions here:
http://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm
Last edited on
Thanks again for the help.

I actually looked at that exact article before making this topic, but I found it unhelpful because it deals with single-dimensional arrays, and also my array is being passed around three different programs (Unit.h, Unit.cpp, Game.cpp).

I also did what you said...and it still refuses to compile.

When you said call the function the way you called it previously, I presume you meant the way I had it in they very first post?

These are my three lines of interest:

From the Unit.h
bool Move(char board[], int x, int y);

From the Unit.cpp
bool Unit:: Move(char *board[], int x, int y)

From the Game.cpp
Alexander.Move(board[r][c], 3, 9);

I think it's worth noting that I also did this (and it doesn't work either)

From the Game.cpp
Alexander.Move(*board, 3, 9);

Last edited on
Everywhere you had defined or implemented the function bool Move, should have the following definition:

bool Unit:: Move(char **board, int x, int y)

Game.cpp:
Alexander.Move((char **)board, 3, 9);

Last edited on
I figured that may be the case, and I had tried it out as well, and it still didn't compile.

This is what I have:

From Unit.h
bool Move(char *board[], int x, int y);

From Unit.cpp
bool Unit:: Move(char *board[], int x, int y)

From Game.cpp
Alexander.Move(board, 3, 9);

Exactly what you said, right??

I don't get why it won't work... I don't know if this will help, but here's the error:

Game.cpp: In function âint main()â:
Game.cpp:39:28: error: no matching function for call to âUnit::Move(char [15][15], int, int)â
Game.cpp:39:28: note: candidate is:
In file included from Game.cpp:12:0:
Unit.h:21:8: note: bool Unit::Move(char**, int, int)
Unit.h:21:8: note: no known conversion for argument 1 from âchar [15][15]â to âchar**â
See my post above
Ahhhhh, finally...it compiled (char, not int haha)

Thank you so much for your help!!! You're a lifesaver.
Topic archived. No new replies allowed.