1

I'm trying to create a game of scrabble and I need to create a board. I created the class Board which has mapTiles() as one of it's functions, it's responsible for creating the base of the board, inserting in the correct tiles the word / letter multipliers. I want this function to return the 2d array so that I'm able to use it in other functions. For ex. on another function I want to do:

Board myBoard;
std::string array[15][15];
array = myBoard.array;

I tried using maps, pointers, vectors and 2d arrays, is there another more efficient way of doing this?

std::string tilesMap[15][15];

std::string mapTiles() {
    for (int cY = 0; cY < 15; cY++) {
        for (int cX = 0; cX < 15; cX++) {
            int cXY = cX + cY;
            double res = pow(cX, 2) + pow(cY, 2);
            tilesMap[cX][cY] = blank;

            if (cX == cY || cXY == 14) { // Sets the diagonals of the board with bonuses
                if (cX == 0 || cX == 14) { // 3 x Word
                    tilesMap[cX][cY] = x3W;
                }

                else if (cX == 5 || cX == 9) { // 3 x Letter
                    tilesMap[cX][cY] = x3L;
                }

                else if (cX == 6 || cX == 8) { // 2 x Letter
                    tilesMap[cX][cY] = x2L;
                }

                else {
                    tilesMap[cX][cY] = x2W; // 2 x Word
                }
            }

            else if (cXY % 7 == 0) {    // 3 x Word
                if (cY == 0 || cY == 7 || cY == 14) {
                    tilesMap[cX][cY] = x3W;
                }
            }

            else if (res == 26 || res == 82 || res == 194 || res == 250) { // 3 x Letter
                if (cY == 1 || cY == 5 || cY == 9 || cY == 13) {
                    tilesMap[cX][cY] = x3L;
                }
            }

            else if (res == 9 || res == 121 || res == 205 || res == 317) { // 2 x Letter outer ring
                if (cY == 0 || cY == 3 || cY == 11 || cY == 14) {
                    tilesMap[cX][cY] = x2L;
                }
            }

            else if (res == 40 || res == 68 || res == 180 || res == 208) { // 2 x Letter 2nd ring

                if (cY == 2 || cY == 6 || cY == 8 || cY == 12) {
                    tilesMap[cX][cY] = x2L;
                }

            }

            else if (res == 58 || res == 170) { // 2 x Letter 3rd ring
                if ((cY == 3) || (cY == 7) | (cY == 11)) {
                    tilesMap[cX][cY] = x2L;
                }
            }
        }
    }
    return tilesMap;
}

The error I'm getting is the following: Error (active) E0415 no suitable constructor exists to convert from "std::string [15][15]" to "std::basic_string, std::allocator>

Drix
  • 11
  • 3
  • 1
    You define the function as returning `void`. Change that to the correct type and it will return it – Sami Kuhmonen Oct 16 '19 at 16:55
  • You could place the array inside a `struct` and return the `struct`. This may be easier than figuring out the return type syntax for a 2d array. (Or maybe better, use `std::vector` or `std::array`). – Thomas Matthews Oct 16 '19 at 17:09
  • 2
    `tilesMap` is a global var, you don't need to return it from the function. – Ripi2 Oct 16 '19 at 17:11
  • To return an array of strings say it so in the definition of the function. Something like `std::string array[15][15] mapTiles()....` – Ripi2 Oct 16 '19 at 17:13
  • 1
    Side note: Returning arrays is... hard. Arrays are always pass by reference (see [What is array decaying?](https://stackoverflow.com/questions/1461432/what-is-array-decaying)) and when the function returns the array goes out of scope making that reference useless. What it referred to is gone. Instead use a [Standard Library Container](https://en.cppreference.com/w/cpp/container) as they allow value semantics. In this case you know the exact size of the array, so `std::array` looks like a good candidate. – user4581301 Oct 16 '19 at 17:26
  • [Usage of returning an array wrapped by a struct](https://stackoverflow.com/questions/27410943/returning-arrays-from-a-function-in-c/27411483#27411483). – PaulMcKenzie Oct 16 '19 at 17:28
  • I recommend you to use a vector of vector of string instead of array. Something like this: `std::vector> test() { std::vector> myString; myString.push_back(std::vector()); myString[0].push_back(std::string("Works")); return myString; }` – Eduardo Fernandes Oct 16 '19 at 18:19
  • If the OP just needs a 15 x 15 string array and needs to pass it around, can't use `std::array` or any STL containers, and pointers are a nightmare to him/her, the next best thing is to wrap it in a struct, IMO. – PaulMcKenzie Oct 16 '19 at 20:18

0 Answers0