CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2005
    Posts
    10

    illegal call of non-static member function -help-

    Im still new to c++, and class's are some thing im still trying to work out but i carnt figure out what im doing rong here.


    : error C2352: 'ThreadClass::StopGoFunc_Go' : illegal call of non-static member function

    : error C2597: illegal reference to data member 'ThreadClass::StopGo' in a static member function


    Code:
    class ThreadClass{
    public:
     
    	ThreadClass()
    	{
    		m_dwThreadID = 0;
    		m_hThread    = 0;
    	}
    
        ~ThreadClass() { 
    		CloseHandle(m_hThread); 
    	}
    	 
    	bool Creat_A_Thread()	 
    	{
    		m_hThread = CreateThread(0,0,StopGoThread,this,0,&m_dwThreadID);    
    		
    		if(!m_hThread)   
    		{
                printf("Thread Failed");
    			return false;
    		}
    		return true;              
    	}
       
    	void StopGoFunc_Go() {  StopGo = true;  }
           void StopGoFunc_Stop() {  StopGo = false;  }
    
    private:
    	DWORD  m_dwThreadID;
    	HANDLE m_hThread; 
            static DWORD WINAPI StopGoThread(LPVOID pvParam);
    	bool StopGo;
    		
    };
    
    DWORD WINAPI ThreadClass::StopGoThread(LPVOID pvParam)
    {
    	StopGoFunc_Go();
    
        while(StopGo = true)
        {
           printf("Im Going"); 
    	   Sleep(2500);
        }
    
    	return 0;
    }
    
    int main()
    {
       ThreadClass MakeThread;
       MakeThread.Creat_A_Thread();
    
       Sleep(10000); 
    
       MakeThread.StopGoFunc_Stop();
    
    	return 0;
    }
    Last edited by BlueOrca; September 21st, 2005 at 01:14 AM.

  2. #2
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: illegal call of non-static member function -help-

    What language have u programmed in before???

    Consider the following class...

    Code:
    class CMyClass
    {
    public:
           int iNum;
           void Increament() {std::cout << ++iNum << endl;}
    };
    For the class, if u want to access the data member (iNum), or if u want to call the member function - Increament()... U MUST CREATE AN OBJECT of the class.

    this is one way how u can create and call the members
    Code:
    CMyClass obj;    //Creates an object
    
    obj.iNum = 5;     //Now u can access the objects members
    
    obj.Increament();    //or call the function
    iNum and Increament are non-static member and method respectively.
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  3. #3
    Join Date
    Aug 2005
    Posts
    10

    Re: illegal call of non-static member function -help-

    I made the object in the main function to call the thread function :
    Or did i do some thing rong ?
    Code:
    int main()
    {
       ThreadClass MakeThread;           <-----------
       MakeThread.Creat_A_Thread();
    ...
    I dont think i need to make an object in the thread function
    DWORD WINAPI ThreadClass::StopGoThread(LPVOID pvParam)
    because that is a member function !

    PS: C++ is my first language.
    Last edited by BlueOrca; September 21st, 2005 at 02:47 AM.

  4. #4
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: illegal call of non-static member function -help-

    The problem is that you have to use a static member function for spawning a thread. However, in static member funciton, the "this" pointer isn't available. Therefore, to access other non-static member in a static member function, you have to pass the address of your object in the LPVOID parameter. When in the function, you have to use this pointer.

    Code:
    DWORD WINAPI ThreadClass::StopGoThread(LPVOID pvParam)
    {
        ThreadClass *p = (ThreadClass *)pvParam;
        p->StopGoFunc_Go();
    
        while(StopGo == true)
        {
           printf("Im Going"); 
    	   Sleep(2500);
        }
    
    	return 0;
    }
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  5. #5
    Join Date
    Aug 2005
    Posts
    10

    Re: illegal call of non-static member function -help-

    Thank you very much

  6. #6
    Join Date
    May 2009
    Posts
    1

    Re: illegal call of non-static member function -help-

    Hi, i'm usig POSIX Threads and i have a problem like that but, i have done all that you say and at the time of compile my program all is ok but when the ejecution arrives at the point where i call the function he quit the program whit this code: First-chance exception in OpenGL.exe: 0xC0000005: Access Violation. Here is my code.


    SRRT::SRRT(Problem *p):RRT(p) {
    p->Tipo=1;
    }

    bool SRRT::Plan()
    {
    int i;
    Planner *Pl;



    MSLVector InitialState(3);

    pthread_t Threads[10]; //Numero de hilos maximos a crear es 10
    READ_PARAMETER_OR_DEFAULT(NumBodies,1);



    for (i=0; i<NumBodies; i++) {

    pthread_create(&Threads[i],NULL,SRRT::Exp_princ,&Pl);
    }


    for (i=0; i<NumBodies; i++) {
    pthread_join(Threads[i],NULL); // wait for threads
    }

    pthread_mutex_destroy(&Pl->CountMutex);
    //pthread_mutex_destroy(&Pl->QueueMutex);


    return true;
    }


    void *SRRT::Exp_princ(void *X)
    {

    Planner *Temp;
    SRRT *S;
    int i,id_hilo,m;
    char cadena[1024];
    ZeroMemory(cadena, 1024);

    // (ThreadClass *)

    Temp = static_cast<Planner *>(X);


    if ((Temp->hilo < 0) || (Temp->hilo > 10))
    {
    Temp->CountMutex=PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_lock(&Temp->CountMutex);
    Temp->hilo=0;
    pthread_mutex_unlock(&Temp->CountMutex);

    }
    else{
    pthread_mutex_lock(&Temp->CountMutex);
    Temp->hilo++;
    pthread_mutex_unlock(&Temp->CountMutex);
    }


    id_hilo=Temp->hilo;

    Temp->Tm1[id_hilo]=NULL;

    wsprintf(cadena, "Hilo No. %d",id_hilo);

    MessageBoxA( NULL,cadena, "Test", MB_OK );


    m=Temp->prueba(3); //---------HERE IS MY PROBLEM

    return (0);

    }



    That part is of a class named SRRT, there is other class named Planner



    int Planner:rueba(int f)
    {
    int i=f;

    for (int v=0; v<f;v++)
    MessageBoxA( NULL,"La prueba", "Test", MB_OK );


    return i;

    }



    I hope you can help me

  7. #7
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: illegal call of non-static member function -help-

    Please see the line highlighted in red. I believe you made a mistake by declaring a pointer and ended up passing an uninitialized pointer to the function, SRRT::Exp_princ().

    Code:
    bool SRRT::Plan()
    {
    int i;
    //Planner *Pl;
    Planner Pl;  //Should be declaring a real instance instead of a pointer.
    
    
    
    MSLVector InitialState(3);
    
    pthread_t Threads[10]; //Numero de hilos maximos a crear es 10
    READ_PARAMETER_OR_DEFAULT(NumBodies,1);
    
    
    
    for (i=0; i<NumBodies; i++) {
    
    pthread_create(&Threads[i],NULL,SRRT::Exp_princ,&Pl);
    }
    
    
    for (i=0; i<NumBodies; i++) {
    pthread_join(Threads[i],NULL); // wait for threads
    }
    
    pthread_mutex_destroy(&Pl->CountMutex);
    //pthread_mutex_destroy(&Pl->QueueMutex);
    
    
    return true;
    }
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured