0

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.

1 Answer 1

1

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);

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

I'm so stupid, thank you very much; i thought int were similar to bool, but i must have forgotten to change it between versions. Thank you again.
@Peter It seems my answer was removed (not by me) which seems cold. No, that's not studpidity, that's "staring at a problem long enough to go blind"-syndrome. I don't know anyone in the business who doesn't get that from time to time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.