Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problems using functions in structs in MSVC++

Problems using functions in structs in MSVC++

Scheduled Pinned Locked Moved C / C++ / MFC
c++jsonhelpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jason Hihn
    wrote on last edited by
    #1

    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]

    D B 2 Replies Last reply
    0
    • J Jason Hihn

      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]

      D Offline
      D Offline
      Dark Angel
      wrote on last edited by
      #2

      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

      J 1 Reply Last reply
      0
      • D Dark Angel

        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

        J Offline
        J Offline
        Jason Hihn
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • J Jason Hihn

          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]

          B Offline
          B Offline
          Ben Burnett
          wrote on last edited by
          #4

          #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" ;)

          J 1 Reply Last reply
          0
          • B Ben Burnett

            #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" ;)

            J Offline
            J Offline
            Jason Hihn
            wrote on last edited by
            #5

            Thanks! this worked after I cleaned up a a few issues...

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups