Problems using functions in structs in MSVC++
-
I am attempting to write a C library of routines for my DVD player. (It talks over a serial port.) I want the library as portable as possible, so I'm attempting to do it in C. I know it'd be alot easier for me to do it with classes, but as far as I'm concerened, C++ is out of the question. I can successfully compile the header below with it's .c file. The problem comes when other C++ modules (I'm trying to use the C library in an MFC app) #include the header. The compiler chokes on the first paranthesis inside the struct definition. I'm thinking it's something having to do with switching into C++ mode. The whole reason why I'm doing this is because I don't want the user to have to mess with a buffer. All they need to provide are callbacks for Open, Close, Read, and Write (reguarldess if they are using J.P. Naughter's serial wrappers, raw win32 api, or Unix file i/o) of the serial port library and users can do something simple like: DVDPlayer.Execute(PLAY); and not have to worry themselves with formatting, writing and returning the response to me for parsing. This way, it can all be handled internally. And in C. [ccode] #pragma once int DVF07Error(struct DVF07*); void DVF07StoreTOC(struct DVF07*, struct toc_type*); void DVF07StoreTrack(struct DVF07*, struct toc_type*, int); void DVF07TraceTOC(struct toc_type*); int DVF07Format(struct DVF07*, int, int); int DVF07Execute(struct DVF07*, int, int); int DVF07ReturnedInt(struct DVF07*); int DVF07SizeofBuf(struct DVF07*); typedef struct DVF07{ /* in c++ files, when this is #included, the ( of int (*Error is */ /* where the compiler chokes, but it compiles fine when */ /* the .c file #includes it */ int (*Error)(struct DFV07 *this); void (*StoreTOC)(struct toc_type*, struct *toc_type); void (*StoreTrack)(struct toc_type*, int i); void (*TraceTOC)(struct toc_type *toc); int (*Format)(struct DVF07 *this, int, int); int (*Execute)(struct DVF07 *this, int, int); int (*ReturnedInt)(struct DVF07 *this); int (*SizeOfBuf)(struct DVF07 *this); /* user provided routines below */ int (*Write)(struct DVF07 *this); int (*Read) (struct DVF07 *this); int (*Open) (struct DVF07 *this, int port); int (*Close)(struct DVF07 *this); char buf[256]; } DVF07_Type; [/ccode]
-
I am attempting to write a C library of routines for my DVD player. (It talks over a serial port.) I want the library as portable as possible, so I'm attempting to do it in C. I know it'd be alot easier for me to do it with classes, but as far as I'm concerened, C++ is out of the question. I can successfully compile the header below with it's .c file. The problem comes when other C++ modules (I'm trying to use the C library in an MFC app) #include the header. The compiler chokes on the first paranthesis inside the struct definition. I'm thinking it's something having to do with switching into C++ mode. The whole reason why I'm doing this is because I don't want the user to have to mess with a buffer. All they need to provide are callbacks for Open, Close, Read, and Write (reguarldess if they are using J.P. Naughter's serial wrappers, raw win32 api, or Unix file i/o) of the serial port library and users can do something simple like: DVDPlayer.Execute(PLAY); and not have to worry themselves with formatting, writing and returning the response to me for parsing. This way, it can all be handled internally. And in C. [ccode] #pragma once int DVF07Error(struct DVF07*); void DVF07StoreTOC(struct DVF07*, struct toc_type*); void DVF07StoreTrack(struct DVF07*, struct toc_type*, int); void DVF07TraceTOC(struct toc_type*); int DVF07Format(struct DVF07*, int, int); int DVF07Execute(struct DVF07*, int, int); int DVF07ReturnedInt(struct DVF07*); int DVF07SizeofBuf(struct DVF07*); typedef struct DVF07{ /* in c++ files, when this is #included, the ( of int (*Error is */ /* where the compiler chokes, but it compiles fine when */ /* the .c file #includes it */ int (*Error)(struct DFV07 *this); void (*StoreTOC)(struct toc_type*, struct *toc_type); void (*StoreTrack)(struct toc_type*, int i); void (*TraceTOC)(struct toc_type *toc); int (*Format)(struct DVF07 *this, int, int); int (*Execute)(struct DVF07 *this, int, int); int (*ReturnedInt)(struct DVF07 *this); int (*SizeOfBuf)(struct DVF07 *this); /* user provided routines below */ int (*Write)(struct DVF07 *this); int (*Read) (struct DVF07 *this); int (*Open) (struct DVF07 *this, int port); int (*Close)(struct DVF07 *this); char buf[256]; } DVF07_Type; [/ccode]
I see one potential problem with that line: "this" is a C++ keyword. Could that be the problem? What happens if you comment that line? "Harland Pepper, would you stop naming nuts" - Harland Pepper
-
I see one potential problem with that line: "this" is a C++ keyword. Could that be the problem? What happens if you comment that line? "Harland Pepper, would you stop naming nuts" - Harland Pepper
I don't think so. I've compiled code before using 'this' in C. I think it only is a problem when __cplusplus is defined. And in this case, it is not - I hope
-
I am attempting to write a C library of routines for my DVD player. (It talks over a serial port.) I want the library as portable as possible, so I'm attempting to do it in C. I know it'd be alot easier for me to do it with classes, but as far as I'm concerened, C++ is out of the question. I can successfully compile the header below with it's .c file. The problem comes when other C++ modules (I'm trying to use the C library in an MFC app) #include the header. The compiler chokes on the first paranthesis inside the struct definition. I'm thinking it's something having to do with switching into C++ mode. The whole reason why I'm doing this is because I don't want the user to have to mess with a buffer. All they need to provide are callbacks for Open, Close, Read, and Write (reguarldess if they are using J.P. Naughter's serial wrappers, raw win32 api, or Unix file i/o) of the serial port library and users can do something simple like: DVDPlayer.Execute(PLAY); and not have to worry themselves with formatting, writing and returning the response to me for parsing. This way, it can all be handled internally. And in C. [ccode] #pragma once int DVF07Error(struct DVF07*); void DVF07StoreTOC(struct DVF07*, struct toc_type*); void DVF07StoreTrack(struct DVF07*, struct toc_type*, int); void DVF07TraceTOC(struct toc_type*); int DVF07Format(struct DVF07*, int, int); int DVF07Execute(struct DVF07*, int, int); int DVF07ReturnedInt(struct DVF07*); int DVF07SizeofBuf(struct DVF07*); typedef struct DVF07{ /* in c++ files, when this is #included, the ( of int (*Error is */ /* where the compiler chokes, but it compiles fine when */ /* the .c file #includes it */ int (*Error)(struct DFV07 *this); void (*StoreTOC)(struct toc_type*, struct *toc_type); void (*StoreTrack)(struct toc_type*, int i); void (*TraceTOC)(struct toc_type *toc); int (*Format)(struct DVF07 *this, int, int); int (*Execute)(struct DVF07 *this, int, int); int (*ReturnedInt)(struct DVF07 *this); int (*SizeOfBuf)(struct DVF07 *this); /* user provided routines below */ int (*Write)(struct DVF07 *this); int (*Read) (struct DVF07 *this); int (*Open) (struct DVF07 *this, int port); int (*Close)(struct DVF07 *this); char buf[256]; } DVF07_Type; [/ccode]
#ifdef __cplusplus extern "C" { #endif // you code here.... #ifdef __cplusplus } #endif
-Ben --------- On the topic of code with no error handling -- It's not poor coding, it's "optimistic" ;) -
#ifdef __cplusplus extern "C" { #endif // you code here.... #ifdef __cplusplus } #endif
-Ben --------- On the topic of code with no error handling -- It's not poor coding, it's "optimistic" ;)Thanks! this worked after I cleaned up a a few issues...