Invalid use of void expression

Hi, again, I am trying to run a void function and pass parameters to it on a thread using std::thread. However, I keep getting compile errors. When I can get it to compile it does not create the file. Thanks in advanced. Here is my code:

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
#include <iostream>
#include <thread>
#include <fstream>
void filehandler(char* amount)
{
    std::fstream output;
    output.open("data.txt");
    for(int i = 0; i <= atoi(amount); ++i)
    {
        output << (i) << ("\n");
    }
    output.close();
    std::cout << ("Done!") << std::endl;
}
int main(int argc, char* argv[])
{
    if(argc > 3)
    {
        char y = 'c';
        if(atoi(argv[1]) == atoi(&y))
        {
            std::cout << ("Writing data to file...");
            std::thread thread1(filehandler(argv[1]));
            thread1.join();
        }
        else if(argc > 3 || atoi(argv[1]) == atoi(&y))
        {
            std::cerr << ("Error: Invalid arguments!") << std::endl;
            return(-1);
        }
    }
    return(0);
}
Last edited on
Line 23 is the culprit.
void filehandler(char* amount) doesn't return anything,
thus you cannot write std::thread thread1(filehandler(argv[1]));
Either pass a reference or a pointer (which you're already doing), or change the function's return type.
It's surprising that you would get it to compile at all, what is it that you did to make it work?
I changed the type to int and I also tried std::thread thread1(filehandler, (argv[1])) both compiled but neither actually passed the argument to filehandler().
Last edited on
1) thread function should be void. (you can have return type but it will be ignored)
2) Correct syntax is std::thread thread1(filehandler, argv[1])
3) You have serious logic problems in your conditions. remove them and look if this would work (it should)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <thread>
#include <fstream>

void filehandler(char* amount)
{
    std::fstream output("data.txt");)
    for(int i = 0; i <= atoi(amount); ++i) {
        output << (i) << ("\n");
    }
    std::cout << "Done!" << std::endl;
}
int main(int argc, char* argv[])
{
    std::cout << ("Writing data to file...");
    std::thread thread1(filehandler, argv[1]);
    thread1.join();
}
TL;DR problem was not in thread, but in messy logic
This is correct:

1
2
// std::thread thread1(filehandler(argv[1]));
std::thread thread1( filehandler, argv[1] );


First verify that this tiny program runs as expected:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <thread>
 
void filehandler( char* arg )
{
    std::cout << "filehandler::arg == " << arg << '\n' ;
}
 
int main( int, char* argv[] )
{
    std::thread thread1( filehandler, argv[0] );
    thread1.join();
}


And then fix the code in the original program.
It tried the test program it did output the contents of argv[0] but mine still does not even say anything when given the wrong arguments. Here is the revised code:

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
#include <iostream>
#include <thread>
#include <fstream>
void filehandler(char* amount)
{
    std::fstream output;
    output.open("data.txt", std::ios::out | std::ios::ate);
    for(int i = 0; i <= atoi(amount); ++i)
    {
        output << (i) << ("\n");
    }
    output.close();
    std::cout << ("Done!") << std::endl;
}
int main(int argc, char* argv[])
{
    if(argc > 3)
    {
        std::string option = (argv[1]);
        if(option == ("-c"))
        {
            std::cout << ("Writing data to file...");
            std::thread thread1(filehandler, argv[2]);
            thread1.join();
        }
        else
        {
            std::cerr << ("Error: Invalid arguments!") << std::endl;
            return(-1);
        }
    }
    return(0);
}
Ok, it now works for the most part. It compiles and passes the arguments to filehandler() which successfully writes it to the file. However, if it is not given any arguments it will segfault.

Here is the code:

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
#include <iostream>
#include <thread>
#include <fstream>
#include <string.h>
void filehandler(const char* amount)
{
    std::ofstream output;
    output.open("data.txt", std::ios::ate);
    for(int i = 0; i <= atoi(amount); ++i)
    {
        output << (i) << ("\n");
    }
    output.close();
    std::cout << ("Done!") << std::endl;
}
int main(int argc, const char* argv[])
{
        if((strcmp(argv[1], "-c") == 0) && (argc == 3))
        {
            std::cout << ("Writing data to file...");
            std::thread thread1(filehandler, argv[2]);
            thread1.join();
        }
        else
        {
            std::cerr << ("Error: Invalid arguments!") << std::endl;  //Should display when invalid argument is given but segfaults instead.
            return(-1);
        }
        return(0);
    }
Last edited on
It took me awhile but I finally fixed it. Yay! It finally complete.
But anyway heres my code:

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
39
40
41
42
43
44
45
#include <iostream>
#include <thread>
#include <fstream>
#include <string.h>
void filehandler(const char* amount)
{
    std::ofstream output;
    output.open("data.txt", std::ios::ate);
    for(int i = 0; i <= atof(amount); ++i)
    {
        if((i % 5 == 0))
        {
            output << (i) << ('\n');
        }
        else
        {
            output << (i) << (' ');
        }
    }
    output.close();
    std::cout << ("Done!") << ('\n');
}
int main(int argc, const char* argv[])
{
    if(argc == 3)
    {
        if((strcmp(argv[1], ("-c")) == 0))
        {
            std::cout << ("Writing data to file...");
            std::thread thread1(filehandler, argv[2]);
            thread1.join();
        }
        else if((strcmp(argv[1], ("-c")) != 0))
        {
            std::cerr << ("Error: Invalid arguments!") << ('\n');
            return(-1);
        }
    }
    else if((argv[2] == nullptr) || (argv[1] == nullptr))
    {
        std::cerr << ("Error: Too few arguments!") << ('\n');
        return(-2);
    }
    return(0);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main( int argc, /*const*/ char* argv[] )
{
    const std::string valid_opt = "-c" ;
    if(argc == 3)
    {
        if( argv[1] == valid_opt )
        {
            std::cout << "Writing data to file..." ;
            std::thread thread1( filehandler, argv[2] ) ;
            thread1.join() ;
        }
        else
        {
            std::cerr << "Error: Invalid arguments!\n" ;
            return -1 ;
        }
    }
    else 
    {
        std::cerr << "Error: Too few arguments!\n" ;
        return -2 ;
    }
    return 0 ;
}
Thanks for the help.
Topic archived. No new replies allowed.