0

I'm working on a C++ project which links to a C library. My C++ class is as follows:

extern "C" {
    struct __struct1__;
    struct __struct2__;
    struct __struct3__;
    struct __struct4__;
}

namespace mynamespace {
   class MyClass : public Parent {
     public:
          ....

     private:
          ....

          __struct1__* s1;
          __struct2__* s2;
          struct __struct3__ s3;
          struct __struct4__ s4;
   };
}

When creating pointers like s1 and s2, everything is OK. But objects don't work well. The following error is generated:

Error using undefined struct

How can I create objects like s3 and s4?

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
hatemfaheem
  • 136
  • 1
  • 12
  • 4
    Make sure `__struct4__` has a full defintion, rather than just a declaration. How is the compiler supposed to know what goes into `__struct4__` if it isn't told!? – StoryTeller - Unslander Monica Jul 31 '13 at 11:47
  • possible duplicate of [undefined C struct forward declaration](http://stackoverflow.com/questions/621356/undefined-c-struct-forward-declaration) –  Jul 31 '13 at 11:49
  • 1
    The compiler always knows the size of a pointer (so it will always be able to create a pointer). The compiler never knows the size of a user-defined type unless its definition has been met (here, you are not defining your types for s3 and s4, hence the compiler doesn't know what to do with them). – Nbr44 Jul 31 '13 at 11:51
  • Great, but how can I tell the compiler what to do? – hatemfaheem Jul 31 '13 at 11:55
  • Provide the definitions of these `struct`s – jrok Jul 31 '13 at 11:58
  • @hatemfaheem surely `__struct3__` and `__struct4__` have a definition somewhere in an include file ? You should then consider including them here, so that the compiler will know about them. – Nbr44 Jul 31 '13 at 12:00
  • @Nbr44 That's right but should I put the includes in the extern block as the library is written in Pure C ? – hatemfaheem Jul 31 '13 at 12:07
  • @hatemfaheem I'm not too sure. I remember that it was the case for the lua bindings library. But I think that would be correct indeed ! – Nbr44 Jul 31 '13 at 12:10
  • 1
    Note that names starting with double-underscore are reserved for 'the implementation' and should not be created by you. – Jonathan Leffler Aug 04 '13 at 20:20

1 Answers1

2

Variable:

__struct1__ *s1

is just of type pointer (to your structure, but still just a pointer), so compiler knows exactly how much memory on stack this variable needs. But in case of:

__struct3__ s3

this variable is of type __struct3__, so without whole definition compiler does not know its size.

zoska
  • 1,684
  • 11
  • 23