Hmm... Not sure how to crack this one. I have this code:
typedef bool execFunctionTyp e(const commandDataType &);
struct commandDataType
{
SymbolSequence Sequence;
string command;
execFunctionTyp e* executer;
};
As you can see, the execFunctionTyp e takes as an argument a
commandDataType struct which contains a pointer to a function of type
execFunctionTyp e. 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 execFunctionTyp e,
and the typedef needs the struct to be defined before it to make sense
of the struct...
What to do?
TIA,
Daniel :) 8 1461
* DanielEKFA: Hmm... Not sure how to crack this one. I have this code:
typedef bool execFunctionTyp e(const commandDataType &);
struct commandDataType { SymbolSequence Sequence; string command; execFunctionTyp e* executer; };
As you can see, the execFunctionTyp e takes as an argument a commandDataType struct which contains a pointer to a function of type execFunctionTyp e. 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 execFunctionTyp e, 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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
DanielEKFA wrote: Hmm... Not sure how to crack this one. I have this code:
typedef bool execFunctionTyp e(const commandDataType &);
struct commandDataType { SymbolSequence Sequence; string command; execFunctionTyp e* executer; };
As you can see, the execFunctionTyp e takes as an argument a commandDataType struct which contains a pointer to a function of type execFunctionTyp e. 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 execFunctionTyp e, 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 execFunctionTyp e(const commandDataType &);
struct commandDataType
{
// other stuff
// ...
execFunctionTyp e* executer;
};
Best
Kai-Uwe Bux
DanielEKFA wrote: Hmm... Not sure how to crack this one. I have this code:
typedef bool execFunctionTyp e(const commandDataType &);
struct commandDataType { SymbolSequence Sequence; string command; execFunctionTyp e* executer; };
As you can see, the execFunctionTyp e takes as an argument a commandDataType struct which contains a pointer to a function of type execFunctionTyp e. 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 execFunctionTyp e, 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 execFunctionTyp e(const commandDataType &);
struct commandDataType
{
SymbolSequence Sequence;
string command;
execFunctionTyp e* 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.
--
Karl Heinz Buchegger kb******@gascad .at
Karl Heinz Buchegger wrote: DanielEKFA wrote: 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.
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 :)
Kai-Uwe Bux wrote: Declare before you define:
struct commandDataType ;
typedef bool execFunctionTyp e(const commandDataType &);
struct commandDataType { // other stuff // ... execFunctionTyp e* 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 :)
Alf P. Steinbach wrote: * DanielEKFA: What to do? 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 :)
DanielEKFA wrote: [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
Kai-Uwe Bux wrote: DanielEKFA wrote:
[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::a ddExecRule("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 :)
Best
Kai-Uwe Bux
--
Why do cats jump out of windows? Because there's love out there! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Roshan Naik |
last post by:
typedef int foo ( foo ); // foo is a pointer-to-function type that takes
another foo as argument and returns an int
I need to achieve the above effect somehow. This is not accepted by any
compiler I have tried, even though I cant spot anything in the standard that
restricts it.
Is there a way to achieve this typedefintion using metaprogramming ? I
noticed that boost::variant does some metaprogramming tricks to allow
|
by: stephen henry |
last post by:
Hi all,
I have a question that I'm having difficulty answering. If I have a
struct:
typedef struct my_struct_tag{
struct my_other_struct *other;
} my_struct_tag
|
by: Merrill & Michele |
last post by:
typedef struct
{
WORD versionNumber;
WORD offset;
}
MENUITEMTEMPLATEHEADER;
This is from vol 5 of unnamed platform's programmer's reference. I could
make this conforming by enclosing everything in a /*...*/ comment and
appending the 'hail world' code. Is there an easier, and, let's say, more
|
by: Immo Birnbaum |
last post by:
Hi,
I'm trying to solve a programming lab assignment for my college C
programming course, but as they taught us two semesters of Java before
teaching us any C, I'm having problems with all the aspects of
pointers. I'd appreciate if anybody could help me with the following
problem:
I tried to learn how to use malloc, free, and the * and & operators.
I started with a few simple lines of code like:
|
by: burn |
last post by:
Hello,
i am writing a program under linux in c and compile my code with make
and gcc.
Now i have 4 files: init.c/h and packets.c/h. Each header-file contains
some:
init.h:
struct xyz
{
| |
by: Ronny Mandal |
last post by:
Consider these two:
Typedef struct { int foo, bar } FooBar;
struct FooBar { int foo, bar };
What woul be the only difference here; just that I can create new
instances by 'Foobar fb, *pfb', and the second demands that I prefix with
struct? struct FooBar fb, *pfb?
|
by: Ian Bush |
last post by:
Hi All,
I'm a bit confused by the following which is causing one of
our user's codes fail in compilation:
typedef struct SctpDest_S;
1) Is this standard ?
2) If so ( or even if not so ! ) what is it supposed to do ?
|
by: Steve Carter |
last post by:
Can someone please explain why the following is not possible?
typedef foobar {
int x;
foobar *next; /* why can't we do this? */
};
Thanks
|
by: cman |
last post by:
What does this kind of typedef accomplish?
typedef struct { unsigned long pte_low; } pte_t;
typedef struct { unsigned long pgd; } pgd_t;
typedef struct { unsigned long pgprot; } pgprot_t
I am familiar with "typedef int NUMBER", but how does it work with
structures.
Tilak
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |