Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Problem with function pointers (c++) with fully problem code

Author: Michel Langeveld

Date: 14:39:37 10/29/03

Go up one level in this thread


On October 29, 2003 at 16:36:04, Gerd Isenberg wrote:

>On October 29, 2003 at 16:16:01, Michel Langeveld wrote:
>
>>On October 29, 2003 at 13:50:38, Gerd Isenberg wrote:
>>
>>>On October 29, 2003 at 12:41:36, Michel Langeveld wrote:
>>>
>>>>>Hi Michel,
>>>>>
>>>>>I guess forEachGame is a static class function.
>>>>>Then you have to use explicite class specifiers in your typedef:
>>>>>
>>>>>typedef void (PGNtoScreen::*on_game_function_type)(char *moves);
>>>>>
>>>>>Gerd
>>>>
>>>>Hi Gerd,
>>>>
>>>>Thanks for looking.
>>>>
>>>>It's actually a static class function. I think the problem is that the compiler
>>>>doesn't know what object to use for calling this function. I made a complete
>>>>code that shows the same problem. Still puzzled.
>>>>
>>>>#include <iostream>
>>>>#include <stdlib.h>
>>>>
>>>>using namespace std;
>>>>
>>>>#define MAXBUF_SIZE 1000
>>>>
>>>>//typedef void (*line_event_type)(char *line);
>>>>typedef void (PGNtoScreen::*line_event_type)(char *line);
>>>>
>>>>class PGNParse
>>>>{
>>>>public:
>>>>	PGNParse()
>>>>	{
>>>>	}
>>>>
>>>        static
>>>>	void parse(char *strTextFile, line_event_type lineEvent)
>>>>	{
>>>>		char line[MAXBUF_SIZE];
>>>>
>>>>		FILE *f = fopen(strTextFile, "r");
>>>>		while (!feof(f))
>>>>		{
>>>>			if (fgets(line, MAXBUF_SIZE, f) == NULL) continue;
>>>>
>>>>			lineEvent(line);
>>>>		}
>>>>	}
>>>>};
>>
>>>What's the reason to use a PGNParse class here at all, so far no data members -
>>>it's more like name spacing.
>>
>>I wanted the make a generic class with all the logic.
>>This example is "isolated". The real class contains more logic and then it makes
>>more sense to have a seperate class.
>>
>>Will look to your other comments.
>
>
>i suggest:
>
>class PGNParse
>{
>public:
>	PGNParse(){}
>	virtual ~PGNParse(){}
>	virtual void lineEvent(char *line) = 0;
>
>        // none static due to call of virtual function
>        //  via (this->*vtable[...])(line)
>	void parse(char *strTextFile)
>	{
>		char line[MAXBUF_SIZE];
>
>		FILE *f = fopen(strTextFile, "r");
>		while (!feof(f))
>		{
>			if (fgets(line, MAXBUF_SIZE, f) == NULL) continue;
>			lineEvent(line);
>		}
>	}
>};
>
>class PGNtoScreen : public PGNParse
>{
>public:
>	PGNtoScreen()	{lines = 0;}
>
>	virtual void lineEvent(char *strLine)
>	{
>	   cout << strLine;
>		lines++;
>	}
>
>	void PGNtoScreen::doit(char *strFileName)
>	{
>	   parse(strFileName);
>	   cout << "Number of lines printed: " << lines << endl;
>	}
>
>private:
>	int lines;
>};

Thanks for helping me out ... will try this.



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.