I'm trying to compile a program but it keeps coming up with errors; I have searched through the forum and I think it has to do with passing by reference, but I can't find where I went wrong.
Here is an extract of the code:
#include <iostream>
#include <thread>
using namespace std;
const int N = 512;
const int N_BUSC = 8;
using VectInt = int[N];
void coord (VectInt v, bool& lectura, bool acabados[N_BUSC], int resultados[N_BUSC]);
int main(int argc, char *argv[]){
bool leido = false;
VectInt v;
int acabados [N_BUSC], resultados [N_BUSC];
...
thread coordinacion (&coord, v, std::ref(leido), acabados, resultados);
...
}
The error it keeps showing is:
/usr/include/c /9/thread: In instantiation of ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (*)(int*, bool&, bool*, int*); _Args = {int (&)[512], std::reference_wrapper<bool>, int (&)[8], int (&)[8]}; <template-parameter-1-3> = void]’:
ej1.cpp:43:74: required from here
/usr/include/c /9/thread:120:44: error: static assertion failed: std::thread arguments must be invocable after conversion to rvalues
120 | typename decay<_Args>::type...>::value,
| ^~~~~
/usr/include/c /9/thread: In instantiation of ‘struct std::thread::_Invoker<std::tuple<void (*)(int*, bool&, bool*, int*), int*, std::reference_wrapper<bool>, int*, int*> >’:
/usr/include/c /9/thread:131:22: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (*)(int*, bool&, bool*, int*); _Args = {int (&)[512], std::reference_wrapper<bool>, int (&)[8], int (&)[8]}; <template-parameter-1-3> = void]’
ej1.cpp:43:74: required from here
/usr/include/c /9/thread:243:4: error: no type named ‘type’ in ‘struct std::thread::_Invoker<std::tuple<void (*)(int*, bool&, bool*, int*), int*, std::reference_wrapper<bool>, int*, int*> >::__result<std::tuple<void (*)(int*, bool&, bool*, int*), int*, std::reference_wrapper<bool>, int*, int*> >’
243 | _M_invoke(_Index_tuple<_Ind...>)
| ^~~~~~~~~
/usr/include/c /9/thread:247:2: error: no type named ‘type’ in ‘struct std::thread::_Invoker<std::tuple<void (*)(int*, bool&, bool*, int*), int*, std::reference_wrapper<bool>, int*, int*> >::__result<std::tuple<void (*)(int*, bool&, bool*, int*), int*, std::reference_wrapper<bool>, int*, int*> >’
247 | operator()()
| ^~~~~~~~
Thank you in advance.
CodePudding user response:
The arguments you pass to coord
do not match its signature.
void coord (VectInt v, bool& lectura, bool acabados[N_BUSC], int resultados[N_BUSC])
^^^^
It should be int
if you want to pass an array of int
to the function - or you should pass an array of bool
to it instead:
bool acabados [N_BUSC]; // not int[N_BUSC]
int resultados [N_BUSC];
thread coordinacion (&coord, v, std::ref(leido), acabados, resultados);