I have an array of 2D arrays which I'm trying to initialise from within a function
I'm doing this for a simple board game I'm trying to make, which has three layers.
I understand you cannot directly assign an array to an array in C++, and that you should use std::copy. Unfortunately I'm having trouble trying to figure out how to implement this.
Here's a simplified version of my program to show what I'm trying to do:
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
|
#include <iostream>
#include <string>
#include <iterator>
using namespace std;
typedef int layer[3][3];
typedef layer board[3];
void initTotal(board * example)
{
layer thisLayer;
for(int count = 0; count < 3; count++)
{
for(int count2 = 0; count2 < 3; count2++)
{
for(int count3 = 0; count3 < 3; count3++)
{
thisLayer[count2][count3] = 0;
// In my actual program, it's much more complicated here.
// Each element of thisLayer is a struct of enums, different for each element
}
}
// *(example + count) = thisLayer;
// If it were possible to assign to arrays, this is what I'd do here.
copy(begin(thisLayer), end(thisLayer), example + count)
// This is where the error occurs
}
}
int main()
{
board example;
initTotal(&example);
return 0;
}
|
The error occurs at:
copy(begin(thisLayer), end(thisLayer), example + count)
This gets me:
//error: 'begin' was not declared in this scope.
//error: 'end' was not declared in this scope.
From what I understand, I have to write a function of the form:
std::copy(first element of thisLayer, last element of thisLayer, the beginning of the correct position in 'example').
I've googled it and I'm not the first to have this problem:
http://stackoverflow.com/questions/19877454/c11-error-begin-is-not-a-member-of-std
A few other websites have people getting the same error using a range for loop, which doesn't apply to me.
I've included "iterator", I've used the std namespace, and I'm sure "thisLayer" is an array. None of those fix the problem though. I've rewritten them as "std::begin" and "std::end", but this gets me:
//Error: 'begin' is not a member of 'std'.
//Error: 'end' is not a member of 'std'.
what can I do here?
Thanks in advance.
Edit:
The full program can be found here:
https://github.com/Matulin/MlinConsole/blob/master/include/board.h
https://github.com/Matulin/MlinConsole/blob/master/src/board.cpp
Edit 2:
Problem solved by making:
typedef int layer[3][3];
typedef layer board[3];
into:
typedef int board[3][3][3];
I'm going to delete this question in an hour. I'm not going to do it immediately in case someone's already typing up an answer (edit 3: and as it turned out, someone was!).