Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Function call missing argument list

Tags:

c++

#include <iostream>
#include <Windows.h>
#include <process.h>
#include <time.h>
#include <conio.h>

using namespace std;
class Klasa
{
public:
    void petla(void* Args)
    {
        time_t tWait = clock() + 4 * CLOCKS_PER_SEC;
        for(;;)
        {
            cout << "Test dzialania watkow" << endl;
            Sleep(1000);
        }
        _endthread();
    }
};
void main()
{
    Klasa* pKlasa = new Klasa;
    time_t tCzas = clock() + 10 * CLOCKS_PER_SEC;
    _beginthread(pKlasa->petla, 0, NULL);
    while(tCzas>=clock())
    {
        cout << " Dziala" << endl;
        Sleep(500);
    }
    getch();
}

Error 1 error C3867: 'Klasa::petla': function call missing argument list; use '&Klasa::petla' to create a pointer to member c:\users\bartek\documents\visual studio 2012\projects\wątki\wątki\source.cpp 26 1 Wątki

This is an error and I don't know What shoud I do cause I can't put () in this beginthread(pKlasa->petla, 0, NULL); Guys please help me :C

like image 244
Bartek Maciąg Avatar asked Dec 08 '25 23:12

Bartek Maciąg


1 Answers

Klasa::petla needs to be declared static if you want it to be the entry point of a thread.

The typical idiom for starting a thread within an object without leaking access to anything important or thread-dangerous looks something like this:

#include <iostream>
#include <Windows.h>
#include <process.h>
#include <time.h>

class Klasa
{
public:
    void Start();

private:
    static void ThreadEntry(void *p);
    void ThreadBody();
};

void Klasa::Start()
{
    _beginthread(Klasa::ThreadEntry, 0, this);
}

void Klasa::ThreadEntry(void *p)
{
    ((Klasa *) p)->ThreadBody();
    _endthread();
    return;
}

void Klasa::ThreadBody()
{
    // do threaded action here
    time_t tWait = clock() + 4 * CLOCKS_PER_SEC;
    for(;;)
    {
        cout << "Test dzialania watkow" << endl;
        Sleep(1000);
    }
}

void main()
{
    Klasa k;
    k.Start();

    time_t tCzas = clock() + 10 * CLOCKS_PER_SEC;
    while(tCzas>=clock())
    {
        cout << " Dziala" << endl;
        Sleep(500);
    }

    char c;
    c << std::cin; // stick to either cin/cout or C-style IO, not both
}

At least, that's how I tend to do it using pthreads. I'd imagine it's basically the same with windows threads.

Also, please try to avoid using Hungarian Notation. It's personal preference, but there are a lot of good arguments for not using it (like the fact that C++ is strongly typed and the type of every variable or function is apparent from its definition).

like image 89
Wug Avatar answered Dec 10 '25 13:12

Wug



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!