I’m trying to write a tic tac toe game in C++, but whenever I run it I get an error message saying:
TicTacToe.cpp: In instantiation of ‘void copy_array(T*, T*) [with T = std::basic_string<char>]’:
TicTacToe.cpp:115:25: required from here
TicTacToe.cpp:93:3: error: no match for ‘operator*’ in ‘**(new_arr + ((sizetype)(((long unsigned int)i) * 8ul)))’
It points to this function:
86 template<class T>
87 void copy_array(T old_arr[], T *new_arr)
88 {
89 int size = sizeof(old_arr)/sizeof(old_arr[0]);
90 for(int i = 0; i < size; i++)
91 {
92 *new_arr[i] = old_arr[i];
93 }
94 }
An this piece of code:
114 string copy[9];
115 copy_array(board, copy);
Could anyone please explain to me what’s causing the error and how to solve it?
That error message is quite strange, it appears to be printing out a transformed version of your code, but I’m pretty sure it’s referring to this line:
Remove the
*and you should be fine.Although of course it won’t function as you expect. Your argument
T old_arr[]is going to act exactly likeT *old_arr. Notably,sizeof(), which you’re relying on to give you the full size of the array, won’t.You could use the following template to get around this:
Or, if you can use C++11, you could switch to
std::array<>, which knows its size.