Typedef A references struct B which references struct A which...

D

DanielEKFA

Hmm... Not sure how to crack this one. I have this code:

typedef bool execFunctionType(const commandDataType&);

struct commandDataType
{
SymbolSequence Sequence;
string command;
execFunctionType* executer;
};

As you can see, the execFunctionType takes as an argument a
commandDataType struct which contains a pointer to a function of type
execFunctionType. Logically this is okay (at least according to my
logic ;), yet the thing won't compile because the struct needs the
typedef to be defined before it to make sense of the execFunctionType,
and the typedef needs the struct to be defined before it to make sense
of the struct...

What to do?

TIA,
Daniel :)
 
A

Alf P. Steinbach

* DanielEKFA:
Hmm... Not sure how to crack this one. I have this code:

typedef bool execFunctionType(const commandDataType&);

struct commandDataType
{
SymbolSequence Sequence;
string command;
execFunctionType* executer;
};

As you can see, the execFunctionType takes as an argument a
commandDataType struct which contains a pointer to a function of type
execFunctionType. Logically this is okay (at least according to my
logic ;), yet the thing won't compile because the struct needs the
typedef to be defined before it to make sense of the execFunctionType,
and the typedef needs the struct to be defined before it to make sense
of the struct...

What to do?

Best:

use a C++ virtual member function instead of a C function pointer.

Worst:

struct commandDataType;

before the typedef.
 
K

Kai-Uwe Bux

DanielEKFA said:
Hmm... Not sure how to crack this one. I have this code:

typedef bool execFunctionType(const commandDataType&);

struct commandDataType
{
SymbolSequence Sequence;
string command;
execFunctionType* executer;
};

As you can see, the execFunctionType takes as an argument a
commandDataType struct which contains a pointer to a function of type
execFunctionType. Logically this is okay (at least according to my
logic ;), yet the thing won't compile because the struct needs the
typedef to be defined before it to make sense of the execFunctionType,
and the typedef needs the struct to be defined before it to make sense
of the struct...

What to do?

Declare before you define:


struct commandDataType;

typedef bool execFunctionType(const commandDataType&);

struct commandDataType
{
// other stuff
// ...
execFunctionType* executer;
};


Best

Kai-Uwe Bux
 
K

Karl Heinz Buchegger

DanielEKFA said:
Hmm... Not sure how to crack this one. I have this code:

typedef bool execFunctionType(const commandDataType&);

struct commandDataType
{
SymbolSequence Sequence;
string command;
execFunctionType* executer;
};

As you can see, the execFunctionType takes as an argument a
commandDataType struct which contains a pointer to a function of type
execFunctionType. Logically this is okay (at least according to my
logic ;), yet the thing won't compile because the struct needs the
typedef to be defined before it to make sense of the execFunctionType,
and the typedef needs the struct to be defined before it to make sense
of the struct...

And here is your thinking flawed.
The typedef doesn't need the struct to be completely declared. All the
compiler needs to know is that somewhere there is a 'struct commandDataType'.
But it doesn't need to know all the details of that struct.

Thus a forward declaration is sufficient:

struct commandDataType;

typedef bool execFunctionType(const commandDataType&);

struct commandDataType
{
SymbolSequence Sequence;
string command;
execFunctionType* executer;
};

Note that in C++ you rarely need function pointers. So it might be
that you are baring up the wrong tree and what you really want is
a class hierarchy with virtual functions.
 
D

DanielEKFA

Karl said:
And here is your thinking flawed.
The typedef doesn't need the struct to be completely declared. All the
compiler needs to know is that somewhere there is a 'struct commandDataType'.
But it doesn't need to know all the details of that struct.

Indeed, you and Kai are right :) Just my brain tiring down after a
dayfull of coding, I suppose.
Note that in C++ you rarely need function pointers. So it might be
that you are baring up the wrong tree and what you really want is
a class hierarchy with virtual functions.

Yep, it's just that I've made a yacc/bison-like command lexer/parser
that you teach command keywords, regular expressions to use for parsing
the commands and their parameters, and a function pointer to use to
execute the command in question. Also, it's being used by threads, so
the function pointers are really useful here.

Thanks,
Daniel :)
 
D

DanielEKFA

Kai-Uwe Bux said:
Declare before you define:


struct commandDataType;

typedef bool execFunctionType(const commandDataType&);

struct commandDataType
{
// other stuff
// ...
execFunctionType* executer;
};

Thanks, Kai-Uwe, I don't know why I didn't remember that... :) Guess
when you learn something new, something old really does fall out ;) At
least when you haven't used the old frequently. Anyway, I've gone an
entirely different way, what I was trying to do wasn't really a very
good idea, just needed to get some clarity.

Cheers,
Daniel :)
 
D

DanielEKFA

Alf said:
* DanielEKFA:

Best:

use a C++ virtual member function instead of a C function pointer.

Hi Alf, thanks for replying :) Not sure exactly how that would work
here? Probably my code snippet is a little too select to make much
sense :)
Worst:

struct commandDataType;

before the typedef.

Why is this bad?

Cheers,
Daniel :)
 
K

Kai-Uwe Bux

DanielEKFA said:
[snip]
Anyway, I've gone an
entirely different way, what I was trying to do wasn't really a very
good idea, just needed to get some clarity.

May I ask what it is that you are trying to do?


Best

Kai-Uwe Bux
 
D

DanielEKFA

Kai-Uwe Bux said:
DanielEKFA said:
[snip]
Anyway, I've gone an
entirely different way, what I was trying to do wasn't really a very
good idea, just needed to get some clarity.

May I ask what it is that you are trying to do?

Of course :) I and three group members are making an ftp-like server and
client. For the command set, instead of writing an advanced lexer/parser
(or write several lexers/parsers) that would need updating if we add a
command, we've made a lexer/parser that understands a simplified regular
expression for syntax checking, so that we can add new commands ad libitum,
like

Preprocessor::addExecRule("GET", "PP*P", &getFunction);

- the preprocessor will then accept commands with the keyword GET if the
parameters are "a path, another path, and zero to many additional paths",
and execute the getFunction passing along the parameters as a symbol
sequence.

My problem before was in getting an elegant, universal implementation that
worked on both the client and the server, as their functions execute
differently (on the client, they execute in main, and add tasks to a global
queue, from which threads receive their tasks, on the server, there's no
queue, and the threads need to do the execution themselves, passing along
their connection file descriptors). I worked around it with a typedef
include. Not the prettiest solution, but I don't see how I can make
templating work in a 100% static class that's never instantiated...

Cheers,
Daniel :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top