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;
}
}
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
}
}
#include <iostream>
#include <vector>
#include <string>
usingnamespace 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::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)