2D Arrays

Hi, I have almost no clue how to use a 2D array.. The start of my project requires a 2D array filled with a private member char array of spaces 24rowsx60cols. And having a set function which will have the ability to set the char array with coordinates (x,y) which will eventually be used to fill in those spaces drawing a circle, square, triangle, etc.

grid.cpp
Grid::Grid()
{
char cGrid [x][y];
// Could cause conflict by only going 0-23 on the grid
for(int i=0; i < x; i++)
{
x = c;
}
for(int i=0; i < y; i++)
{
y = c;
}
}

void Grid::set(int x, int y, char c)
{
Grid();
}

void Grid::print()
{
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; y++)
{
cout << cGrid[i][y] << ' ';
}
cout << endl;
}
}
Last edited on
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
int main()
{
    using row = char[60] ; // row is an alias for 'array of 60 char'
    // the 60 char elements are conceptually numbered as 0...59.
    // a particular element (char) can be accessed using the subscript operator []

    // for instance,
    row a_row ; // a_row is an 'array of 60 char'
    a_row[0] = ' ' ; // assign to the char at position zero (the first position)
    a_row[24] = ' ' ; // assign to the char at position 24
    a_row[59] = ' ' ; // assign to the char at position 59 (the last position)

    // when an array contains elements which are themselves arrays, we may call it a multi-dimensional array.
    // for instance
    row grid[24] ; // grid is an 'array of 24 row'
    // the 24 row elements are conceptually numbered as 0...23.
    // we can access a particular row with the subscript operator []
    // for instance,
    row& row4 = grid[4] ; // grid[4] is the element (a row) at position 4
    // the row is an array; the subscript operator can be applied on it
    // (arrays can't be copied; row4 is an alias for (reference to) the row at position 4)
    row4[7] = ' ' ; // assign to the char at position 7 in row4

    // or:
    grid[4][7] = ' ' ; // assign to the char at position 7 in the row at position 4

    {
        // we could have also written:
        char grid[24][60] ; // grid is an "array of 24 'array of 60 char' "
        grid[4][7] = ' ' ; // assign to the char at position 7 in the row at position 4
    }
}
grid.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int x = 24;
int y = 60;
char c = ' ';

class Grid
{
	public:
	Grid();
	void set(int x, int y, char c);
	void print();

	private:
	char cGrid [24][60];
	
};

grid.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
Grid::Grid()
{
	for (int i=0; i<x; i++)
	{
    	for (int j=0; j<y; j++)
    	{
      		cGrid[i][j]=' ';
    	}
    }
}

void Grid::set(int x, int y, char c)
{

}

void Grid::print()
{
	for (int i = 0; i < x; i++)
    {
        for (int j = 0; j < y; y++)
        {
            cout << cGrid[i][y] << ' ';
        }
        cout << endl;
    }
}

-------------------------
Now I am getting the error;
duplicate symbol _x in:
/var/folders/27/y0txfrl95s95kzqvvqrwdk4c0000gn/T/grid-a0de44.o
/var/folders/27/y0txfrl95s95kzqvvqrwdk4c0000gn/T/maintest-fa9830.o
duplicate symbol _y in:
/var/folders/27/y0txfrl95s95kzqvvqrwdk4c0000gn/T/grid-a0de44.o
/var/folders/27/y0txfrl95s95kzqvvqrwdk4c0000gn/T/maintest-fa9830.o
duplicate symbol _c in:
/var/folders/27/y0txfrl95s95kzqvvqrwdk4c0000gn/T/grid-a0de44.o
/var/folders/27/y0txfrl95s95kzqvvqrwdk4c0000gn/T/maintest-fa9830.o
ld: 3 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
You should probably include grid.h in grid.cpp for starters.
Last edited on
1
2
3
int x = 24;
int y = 60;
char c = ' ';


These names have external linkage.
If you want to have them in the header file, declare them as constants: constants have internal linkage.
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=41

The header requires a #include guard: https://en.wikipedia.org/wiki/Include_guard

Avoid gratuitous #includes and using directives in header files.

Your header file should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef GRID_H_INCLUDED_
#define GRID_H_INCLUDED_

// #include <iostream>
// #include <vector>
// #include <string>
// using namespace std;

const int x = 24;
const int y = 60;
const char c = ' ';

class Grid
{
	public:
	    Grid();
	    void set(int x, int y, char c);
	    void print();

	private:
	    char cGrid [x][y]; // avoid magic numbers
            // https://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants
}; 
#endif // #define GRID_H_INCLUDED_ 


Read this: http://www.cplusplus.com/articles/Gw6AC542/
Topic archived. No new replies allowed.