0

Im a NOOB with programming and Im really stumped. I get "array type has incomplete element and field 'status' has incomplete type" errors when i compile this code. I have this linked with another pile of code "which is thankfully error free". the errors are identified in this section so any help will be appreciated. Thank you. here's the C code

struct name;
struct book;
struct Library{
    struct Book collection[100];
    struct person patrons[100];
    int totalBooks;
    int totalPatrons;
};
struct person{
    char first[32];
    char last[32];
    enum Stat status;
};

struct Book{
    char title[32];
    char author[32];
    int id;
    int year;
    int status;

};
enum Stat{ACTIVE=1, INACTIVE=2, CHECKED_OUT=3, CHECKED_IN=4, UNDER_REPAIR=5, LOST=6};
~            

3 Answers3

2

Looks like you've come from a C background - unfortunately it isn't valid C# code.

  • Arrays are defined as char[] first; (also size isn't relevant at this point)
  • Enums do not need an enum prefix when defining the variables. Same with structs.
  • You don't need to declare structs for name and book

In fact, I think you're probably wanting to use a string variable instead of char arrays.

struct Library{
    Book[] collection;
    Person[] patrons;
};

struct Person{
    string first;
    string last;
    Stat status;
};

struct Book{
    string title;
    string author;
    int id;
    int year;
    int status;

};
enum Stat{ACTIVE=1, INACTIVE=2, CHECKED_OUT=3, CHECKED_IN=4, UNDER_REPAIR=5, LOST=6};
Paul Mason
  • 1,129
  • 8
  • 15
  • Thanks for the reply. Actually, I started a month ago. What I'm trying to do is include an array of book structures in the library structure. And would you mind explaining your enum answer again? Thanks – user1006281 Oct 21 '11 at 01:11
  • 1
    To be honest, I'd probably go back to the drawing board as I don't think structs are the right choice here. I'd look at changing everything to classes and properties, but then that's my personal preference. – Paul Mason Oct 21 '11 at 01:14
  • Oops - pressing enter posts a comment! In your code you were defining the enum field with an `enum` prefix - you don't need to do this in C# (but you did in C). Another point, Enums in C# generally are camel cased, and in this case - do not need a value assigned to each position. – Paul Mason Oct 21 '11 at 01:15
  • Thanks a lot. But I have helper functions in another file that require (and use) the structures to work. I'll fry my brain some more now. – user1006281 Oct 21 '11 at 01:16
  • wait a sec.. is c the same as c#?? – user1006281 Oct 21 '11 at 01:17
  • No - C is a different language to C#. It does share a few common keywords etc however are vastly different. – Paul Mason Oct 21 '11 at 01:19
  • -1 Good comment but there was confusion about the language in question here. – cjcurrie Feb 05 '13 at 08:50
1

There are basically two things wrong with the section of code you showed that the compiler is complaining about:

  1. C is case sensitive: struct book and struct Book are two different types.
  2. In C, you cannot refer to a type until it has been declared; that is, you cannot define a field of type enum Stat before you define enum Stat.

The actual problem, then, is that the compiler doesn't know what a struct Book is at the point where you try to define an array of them. Similarly, it doesn't know what an enum Struct is at the point where you define a field of that type.

(Mostly unimportant tangent: The reason you are getting the "incomplete type" errors instead of something slightly more useful is because the compiler allows you, in certain cases, to use struct types that you don't actually have the full definition of, but only if you use them through so-called "opaque" pointers (that is, you never actually use the type, you just pass pointers to them around.) In your case you are telling the compiler you want an array of struct Book, which requires a completely define type, which you don't have.)

To fix it you just need to reorder your type definitions so that none of them are used before they're defined, and use consistent casing throughout. Also, while it's legal to continue to refer to struct foo and enum bar in the rest of your program, most people would create a typedef (basically, type aliases) instead. For example:

typedef enum tagStat {
    ACTIVE=1, 
    INACTIVE=2, 
    CHECKED_OUT=3, 
    CHECKED_IN=4, 
    UNDER_REPAIR=5, 
    LOST=6
} Stat;

typedef struct tagPerson {
    char first[32];
    char last[32];
    Stat status;
} Person;

typedef struct tagBook {
    char title[32];
    char author[32];
    int id;
    int year;
    int status;
} Book;

typedef struct tagLibrary {
    Book collection[100];
    Person patrons[100];
    int totalBooks;
    int totalPatrons;
} Library;
Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117
  • nothing, it's just convention so that your struct and typedef names are different. The part after the `struct` in a struct definition is called a tag. So the statement is parsed like: ; See http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c for an explanation of typedefs in C – Michael Edenfield Oct 21 '11 at 02:32
  • struct Library{Book collection[100];.....}; correct? (assuming there is proper indentation). – user1006281 Oct 21 '11 at 02:34
  • woow. thank you very much. it actually got rid of the problem. But do you mind if i disturb you for a bit? how to I associate the 'status' variable with the enums? like using status to show wether the person is ACTIVE or INACTIVE – user1006281 Oct 21 '11 at 02:39
0

It looks like you are trying to use the Stat enum in your definition of person before you declare/define it. Add enum Stat; where you have the struct statements at the top of the file.

Ralfonso
  • 1,614
  • 15
  • 30