Thread: Error: cannot call member function without object

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    44

    Error: cannot call member function without object

    We have been stuck on this for an hour now.

    We're getting an error:
    error: cannot call member function 'int GameEvent::makeargv(const char*, const char*, char***)' without object
    Here's our code:
    Code:
    GameEvent* GameEvent::fromString(const Game &g, const string &s){
      const char *inputHold=s.c_str();
      int numArgs=makeargv(inputHold, " ", &buffer);
    
    }
    We have also tried:
    Code:
    GameEvent* GameEvent::fromString(const Game &g, const string &s){
      int sLength=s.size();
        	char* inputHold;
    	string holder=s;
    	inputHold=(char*)malloc(sizeof(char)*sLength);
    	int i;
    	for (i=0;i<sLength; i++) {
    		inputHold[i]=s[i];
    		cout <<inputHold[i];
    	}	
       int numArgs=makeargv(inputHold, " ", &buffer);
    }
    The declaration for makeargv is:
    Code:
    int makeargv(const char *s, const char *delimiters, char ***argvp)
    Any help would be great. Thank you.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is fromString a static function? I can't seem to duplicate your error.

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    is fromString() static?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    it is static

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then you've got some sort of design problem I guess. (A static function surely can't call a nonstatic member function, since you have to call a (normal) member function from an object.) (EDIT: I suppose you could pass an object into your static function, but at that point we're defeating the purpose I think.)

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    I've looked and asked else where, and I just can't find a solution. Is it the makeargv that's the problem?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by TIMBERings View Post
    I've looked and asked else where, and I just can't find a solution. Is it the makeargv that's the problem?
    I guess you could look at it that way. You have (hopefully) made a deliberate design choice to write a static function -- and that comes at a cost. Static functions are somewhat crippled in terms of what parts of the class you can access -- essentially, you can't access anything that belongs to an instance of the class, only those elements that belong to the class-as-a-whole (i.e., the other static elements).

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    fromString() is a static function. The compiler would complain about a call of markargv() if makeargv() is non-static. Non-static function need to be invoked for an object eg. int numArgs=some_object.makeargv(inputHold, " ", &buffer);
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem defining structure
    By MTK in forum C Programming
    Replies: 12
    Last Post: 09-08-2009, 03:26 PM
  2. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 07:42 AM
  3. illegal call of non-static member function
    By JackR in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2007, 11:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

Tags for this Thread