Incomplete type specification

This is a discussion on Incomplete type specification within the C++ Programming forums, part of the General Programming Boards category; I'm working my way through Eckel's "Thinking in C++ " book at the moment and if I interpreted it correctly, ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    20

    Incomplete type specification

    I'm working my way through Eckel's "Thinking in C++ " book at the moment and if I interpreted it correctly, the following code should be incorrect (compiler-warning for the "void g(X ex);"):

    Code:
    struct X;
    
    struct Y {
      void f(X*);
      void g(X ex);
    };
    
    
    struct X { // Definition
    private:
      int i;
    public:
      friend void Y::f(X*); 
      friend void Y::g(X);
      void initialize();
     };
    
    
    void Y::g(X ex) {
      ex.i = 3333;
    }
    But at least the GCC compiler can work with this. According to the book, the Y::g declaration is supposed to fail because the compiler needs to know "the entire structure definition of X, to know the size and how to pass it", whereas Y::f only takes the address of an X object and the compiler can handle this without the definition of X.
    My question is if the code above is valid (would work on other compilers as well) or if it's just GCC that can handle this.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    edit: nevermind, i grossly misread your post
    Last edited by misplaced; 12-19-2004 at 08:58 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    This problem would only occur if you didn't have
    Code:
    struct X;
    at the start.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Actually I think this particular error would occur if he didn't have this:

    Code:
    struct X { // Definition
    private:
      int i;
    public:
      friend void Y::f(X*); 
      friend void Y::g(X);
      void initialize();
     };
    The error you describe happens when you are attempting to create an instance of an object without your compiler knowing what the object will look like. It would be like me asking you for a car that I like and giving you no further detail than that.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I would say that a function declaration does not need complete types for parameters and return value, but I found no place in the standard to back me up.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah even without the support from the standard, it seems to be the typical behavior of many compilers.

  7. #7
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    It would be a error if you try do define functions inside the class, for example:
    Code:
    struct X;
    
    struct Y {
      void f(X*);
      void g(X ex){ ex.i = 3333; }
    };
    
    
    struct X { // Definition
    private:
      int i;
    public:
      friend void Y::f(X*); 
      friend void Y::g(X);
      void initialize();
    };

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    > It would be a error if you try do define functions inside the class
    Well...I'd say no, but you are right in this instance. But the reason for that it being erroneous in this context is because your compiler has no idea what an X is. Inlining functions is otherwise not illegal.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Well, the error is in the define part, not in the inside the class part.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yep The only way that would work is if you define struct X before defining struct Y. Compilers are not mind readers. One other little detail, function would be altering a local copy of the X object, thus even if it were written correctly enough to compile, I don't think it would do what you intended for it to do.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I doubt Mortissus intended it to do anything
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    It was just an example of a kind of error that I had before.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Figured as much, its just good to keep people from feverishly copying code that is known to not work, then later posting:

    I need help!

    Code:
    struct X;
    
    struct Y {
      void f(X*);
      void g(X ex){ ex.i = 3333; }
    };
    
    
    struct X { // Definition
    private:
      int i;
    public:
      friend void Y::f(X*); 
      friend void Y::g(X);
      void initialize();
    };
    wont work!! I need this working before class starts in 37 seconds! Please help!!

  14. #14
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    Thanks for the input.

    So it seems to be safe to ignore the book's statement that this is only possible with pointers to X. Or might this be a case of "you can do it, but you shouldn't do it"? Though I wouldn't see why...
    Last edited by New++; 12-20-2004 at 03:32 AM.

  15. #15
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by New++
    I'm working my way through Eckel's "Thinking in C++ " book at the moment and if I interpreted it correctly, the following code should be incorrect (compiler-warning for the "void g(X ex);"):

    Code:
    struct X;
    
    struct Y {
      void f(X*);
      void g(X ex);
    };
    
    
    struct X { // Definition
    private:
      int i;
    public:
      friend void Y::f(X*); 
      friend void Y::g(X);
      void initialize();
     };
    
    
    void Y::g(X ex) {
      ex.i = 3333;
    }
    But at least the GCC compiler can work with this. According to the book, the Y::g declaration is supposed to fail because the compiler needs to know "the entire structure definition of X, to know the size and how to pass it", whereas Y::f only takes the address of an X object and the compiler can handle this without the definition of X.
    My question is if the code above is valid (would work on other compilers as well) or if it's just GCC that can handle this.
    Unless my brain is deader than usual in yor example the compielr does indeed know "the entire structre definition of X" (it doesn't matter if some of it is private)

    Quote Originally Posted by Mortissus
    It would be a error if you try do define functions inside the class, for example:
    Code:
    struct X;
    
    struct Y {
      void f(X*);
      void g(X ex){ ex.i = 3333; }
    };
    
    
    struct X { // Definition
    private:
      int i;
    public:
      friend void Y::f(X*); 
      friend void Y::g(X);
      void initialize();
    };
    In this case the compiler doesn't know the definition of X and therefore throws an error when compiling Y::g().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. arithmetic on pointer to an incomplete type?
    By Volair in forum C Programming
    Replies: 4
    Last Post: 11-19-2006, 05:53 PM
  3. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21