Function reference problems

This is a discussion on Function reference problems within the C++ Programming forums, part of the General Programming Boards category; I'm trying to reference a function to pthread_create, and i am using the exact same syntax as elsewhere, but it ...

  1. #1
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281

    Function reference problems

    I'm trying to reference a function to pthread_create, and i am using the exact same syntax as elsewhere, but it doesn't work. Whats the problem?
    Errors:
    Code:
    g++ -pthread -c parser.cpp
    parser.cpp: In method `void ircParser::init (char *, ircserver, 
    commander)':
    parser.cpp:34: no matches converting function `runThread' to type `void 
    *(*) (void *)'
    parser.h:34: candidates are: void *ircParser::runThread (void *)
    make: *** [parser.o] Error 1
    Here is the code:
    Code:
    // parser.cpp
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <netdb.h>
    //	#include <regex.h>	// Regular Expression (POSIX) Matching
    #include <fnmatch.h>		// Shell-like Regular Expressions
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <iostream>
    #include <fstream>		// For cout redirecting
    #include <string>
    #include <fcntl.h>
    #include <sys/ioctl.h>		// ioctl
    #include <getopt.h>
    #include "ircserver.h"
    //#include "sockfunc.h"
    //#include "ircfunc.h"
    #include "nemoirc.h"
    #include "logger.h"
    #include "parser.h"
    
    // Externs
    extern logger logfile;
    
    void ircParser::init(char *iB, ircserver iIS, commander iC) {
    	serv = iIS;
    	buf = iB;
    	cmd = iC;
    
    	pthread_t p;
            int     data = 123,
                    rc;
    	init(buf, serv, cmd);
            rc = pthread_create( &p, NULL, run, &data );
            if( rc != 0 ) {
                    fprintf( stderr, "pthread_create: %s\n", strerror( rc ) ); 
            } else {
                    logfile << logfile.stamp() << "** ERROR STARTING PARSE **" << logfile.endl;
            }
    }
    
    void *ircParser::run(void *arg) {
            rawMsg   message;
            char    *type;
            if (message.msgParser(buf, serv) != 0) return (void *)NULL;
            if (strcmp("PRIVMSG", message.getCommand()) == 0)
                    logfile << logfile.stamp() << "<" << message.getNick() << "> " << message.getMessage() << logfile.endl;
            if (strcmp("NOTICE", message.getCommand()) == 0)
                    logfile << logfile.stamp() << "-" << message.getNick() << "- " << message.getMessage() << logfile.endl;
            cmd.setMessage(message.getSender(), message.getRecipient(), message.getMessage(), message.getCommand(), serv);
            cmd.parsePrivmsg();
            cmd.parseNotice();
            cmd.parseSpecial();
            return (void *)NULL;
    }
    Code:
    // parser.h
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <netdb.h>
    //	#include <regex.h>	// Regular Expression (POSIX) Matching
    #include <fnmatch.h>		// Shell-like Regular Expressions
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <iostream>
    #include <fstream>		// For cout redirecting
    #include <string>
    #include <fcntl.h>
    #include <sys/ioctl.h>		// ioctl
    #include <getopt.h>
    #include "ircserver.h"
    //#include "sockfunc.h"
    //#include "ircfunc.h"
    #include "nemoirc.h"
    #include "logger.h"
    
    // External declarations
    //extern logger logfile;
    //parsedArgs opts;
    
    class ircParser {
    	private:
    		//logger logfile;
    		ircserver serv;
    		char *buf;
    		commander cmd;
    		void   *run( void *arg );
    
    	public:
    		void   init(char *, ircserver, commander);
    };
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  2. #2
    cgoat
    Guest
    If you want to use a member function for pthread_create, it has to be a static member. That means you can't access anything in the class that is non-static without passing in the this pointer as the argument.

    Code:
    class ircParser
    {
    public:
       static void* run(void* parg);
    
    private:
       int m_data;
    };
    
    void* ircParser::run(void* parg)
    {
       ircParser *pThis = reinterpret_cast<ircParser*>(parg);
    
       // Use data
       pThis->m_data = 5;
    
       while(pThis->m_runthread)
       {
          // do stuff
       }
    
       return 0;
    }
    
    // To create the thread
    pthread_create( &p, NULL, run, reinterpret_cast<void*>(this));
    SOP for member function threads.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The pThis-> is tiresome. Use it only to call a real member function that does the work:
    Code:
    class ircParser
    {
    public:
       static void* do_run(void* parg);
    
       void *run();
    
    private:
       int m_data;
    };
    
    void* ircParser::do_run(void* parg)
    {
       ircParser *pThis = reinterpret_cast<ircParser*>(parg);
    
       // Call real method
       return pThis->run();
    }
    
    void *ircParser::run()
    {
      // do stuff
    }
    
    // To create the thread
    pthread_create( &p, NULL, do_run, reinterpret_cast<void*>(this));
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Problem with Template Function and overloaded equality operator
    By silk.odyssey in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2004, 04:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21