OOP with Incomplete types problem

This is a discussion on OOP with Incomplete types problem within the C Programming forums, part of the General Programming Boards category; Hi, I'm new here but i've used this forum since i've started programing in C some month ago (btw, my ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    8

    OOP with Incomplete types problem

    Hi,

    I'm new here but i've used this forum since i've started programing in C some month ago (btw, my nick is nosila, can a moderator / admin change that please? My bad), i have a school project to finish, it is mainly about creating a database program using list's, etc... I tryed to go over the top with this learning incomplete types and how i can use them to have some pseudo OO in C (i've experience with Java so i'm work really well with this) but i'm having all sorts of trouble doing it... So, what i have now is:

    (this is not my actual project, just trying to resume it with the information i found more relevant)

    Code:
    Paint.h
    
    #include Main.h typedef struct _paint Paint;
    Paint.c
    #include Paint.h struct _paint{
    char * paint_color; int paint_number;
    };
    Car.h
    #include Main.h typedef struct _car Car;
    Car.c
    #include Car.h struct _car{
    Paint * paint; char * brand; char * model;
    };
    Main.h
    #include <stdio.h> #include <stdlib.h> #include "Paint.h" #include "Car.h"


    All of the header files have the #ifndef so the header is only defined once

    Problem:
    -How can i have a function in car that return the *paint???

    if i have it like this

    Paint * car_getPaint(Car * car);

    it gives me errors in header file (in the beginning of this function signature) saying

    error C2143: syntax error : missing '{' before '*'

    ------------


    I've get this error in my project and in a "test" project i have created to test this kind of stuff... Anyone knows how to solve this??

    PS: i've written this code here without testing so it may be possible i am missing some small things like ; or smtg!
    Last edited by nosil; 01-04-2011 at 05:27 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't actually have anything called 'Paint' in your example. Consider:
    Code:
    /* foo.h */
    typedef struct foo
    {
        ...stuff...
    } FOO;
    
    /* some other file */
    #include "foo.h"   /* now you can use FOO */
    
    FOO *myfunthatreturnsfoo( void )
    {
        ...
        return athingoffootype;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Quote Originally Posted by quzah View Post
    You don't actually have anything called 'Paint' in your example. Consider:
    Code:
    /* foo.h */
    typedef struct foo
    {
        ...stuff...
    } FOO;
    
    /* some other file */
    #include "foo.h"   /* now you can use FOO */
    
    FOO *myfunthatreturnsfoo( void )
    {
        ...
        return athingoffootype;
    }

    Quzah.
    But i'm including Main.h wish is including paint And car so, shouldn't all the codefiles have acess to all the typedef's ??

    Also, i've tryed that in my project (including only the headers i used) and it didnt work (this is a wild guess, maybe its something about the incomplete type? )

    One thing i noticed was, in Main.h, if i included Car.h 1st Paint.h 2nd, i got 1 error (the one i posted already), but if i had #include Paint.h 1st then Car.h 2nd (wish is more logical for me) it gave me 3 of the same error...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Did you intend to do something like
    Code:
    typedef struct _paint Paint;
    somewhere? Just including "someobject.h" doesn't automagically give you something called "someobject" -- you have to actually declare it in the code.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Quote Originally Posted by tabstop View Post
    Did you intend to do something like
    Code:
    typedef struct _paint Paint;
    somewhere? Just including "someobject.h" doesn't automagically give you something called "someobject" -- you have to actually declare it in the code.
    Sorry, forgot to change that, i do that in the Paint.h ...

    Or are you saying i have to typedef it in every place i use that type???

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why aren't you defining your structures in the header files? Anyway, car.h/.c doesn't have any reference or knowledge of paint.h, because it's not included for car.h/.c to see.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Usual idiom:

    foo.h // interface
    Code:
    #ifndef FOO_INCLUDED                // include guard
    #define FOO_INCLUDED
    enum { MAX_FOO_SIZE = 100 };
    
    typedef struct foo foo;
    foo *new_foo(int bar,char *s);
    #endif
    foo.c // implementation

    Code:
    #incude "foo.h"
    
    struct foo {
       int bar;
       char *s;
    };
    foo *new_foo(int n,char *str) {
     ....
    }

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Quote Originally Posted by quzah View Post
    Why aren't you defining your structures in the header files? Anyway, car.h/.c doesn't have any reference or knowledge of paint.h, because it's not included for car.h/.c to see.


    Quzah.
    Incomplete types as abstractions

    That way you hide the struct implementation from the other codefiles, they have to use the functions to get the struct values, so... this is how i learn you could get OO in C

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Quote Originally Posted by Bayint Naung View Post
    Usual idiom:

    foo.h // interface
    Code:
    #ifndef FOO_INCLUDED                // include guard
    #define FOO_INCLUDED
    enum { MAX_FOO_SIZE = 100 };
    
    typedef struct foo foo;
    foo *new_foo(int bar,char *s);
    #endif
    foo.c // implementation

    Code:
    #incude "foo.h"
    
    struct foo {
       int bar;
       char *s;
    };
    foo *new_foo(int n,char *str) {
     ....
    }
    Yes, that always works but when i try to use 2 typedef's of incomplete type structs that i created, it gives-me error


    Tried to typedef both structs in the header where i use them, and it works, like

    typedef struct _car Car;
    typedef struct _paint Pain;


    but, do i really have to do that in EVERY header i use ? Shouldn't the #include combo take care of that??? This is BS, wasted a full day and a half thinking that doing that was plain stupid...
    Last edited by nosil; 01-04-2011 at 06:32 PM.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Quote Originally Posted by nosil View Post
    Incomplete types as abstractions

    That way you hide the struct implementation from the other codefiles, they have to use the functions to get the struct values, so... this is how i learn you could get OO in C
    You're not really getting OO in C. An OO implementation on par with something like C++ or Java is pretty much impossible. You can implement a few of the features and design patterns (e.g. factory) this way. Nevertheless, hiding struct details can be useful. Bayint Naung gave the classic example I suggest you follow.

    I would also avoid your crazy circular #include statements, with Main.h including Paint.h and Car.h, each of which include Main.h. Make things simple and logical. If the code in Car.c needs to know about stuff in Paint.h, include it. If not, don't. Same goes for Paint.h and Main.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by nosil View Post
    but, do i really have to do that in EVERY header i use ? Shouldn't the #include combo take care of that???
    It does, as you have it written in this thread (in that I have pasted into a set of files here and it compiles beautifully). If it's not working for you in your files, it's because you're not doing what you say you're doing.

  12. #12
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Quote Originally Posted by anduril462 View Post
    You're not really getting OO in C. An OO implementation on par with something like C++ or Java is pretty much impossible. You can implement a few of the features and design patterns (e.g. factory) this way. Nevertheless, hiding struct details can be useful. Bayint Naung gave the classic example I suggest you follow.

    I would also avoid your crazy circular #include statements, with Main.h including Paint.h and Car.h, each of which include Main.h. Make things simple and logical. If the code in Car.c needs to know about stuff in Paint.h, include it. If not, don't. Same goes for Paint.h and Main.
    Yes i know that, but at least is the most approximate OO i can get at least, in terms of encapsulation and data hiding...

    Hum ok gonna change that I did that thinking that those typedef's would be "global", that way i could use any typedef anywhere..

    Quote Originally Posted by tabstop View Post
    It does, as you have it written in this thread (in that I have pasted into a set of files here and it compiles beautifully). If it's not working for you in your files, it's because you're not doing what you say you're doing.
    Just for the record, what compiler are you using? Tomorrow i will upload my test project where i replicate that...

    Thanks for all the help,

    -NoSila
    Last edited by nosil; 01-04-2011 at 08:22 PM.

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Solved, was a problem with the #includes, just had to put them right and now everything works like a charm...

    Thanks everyone for the kind help,

    -NoSila

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 11:57 AM
  2. problem passing cstring, incompatible types
    By c_weed in forum C++ Programming
    Replies: 12
    Last Post: 10-26-2010, 05:33 PM
  3. OOP with windows message loops problem
    By cloudy in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2006, 12:09 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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