why this code not work

Discussion in 'C++' started by Jim Johnson, Mar 11, 2008.

  1. Jim Johnson

    Jim Johnson Guest

    why the struct must be
    ===================
    typedef struct {
    ... etc ..
    } SetupRecord;

    CANNOT be ...
    ===================
    struct {
    ... etc ..
    } SetupRecord;

    ===================
    static const SetupRecord g_SetupRecordTable[] = {
    { TEST1, "gggg"},
    };

    ===================
    1>main.cpp
    1>d:\remote3\remote\remotedemo\app\remotedemo\main.cpp(10) : error
    C4430: missing type specifier - int assumed. Note: C++ does not
    support default-int
    1>d:\remote3\remote\remotedemo\app\remotedemo\main.cpp(10) : error
    C2146: syntax error : missing ';' before identifier
    'g_SetupRecordTable'
    1>d:\remote3\remote\remotedemo\app\remotedemo\main.cpp(10) : error
    C2373: 'SetupRecord' : redefinition; different type modifiers
    1> d:\remote3\remote\remotedemo\app\remotedemo\main.cpp(8) :
    see declaration of 'SetupRecord'
    1>d:\remote3\remote\remotedemo\app\remotedemo\main.cpp(10) : error
    C4430: missing type specifier - int assumed. Note: C++ does not
    support default-int
    1>d:\remote3\remote\remotedemo\app\remotedemo\main.cpp(11) : error
    C2078: too many initializers
    Jim Johnson, Mar 11, 2008
    #1
    1. Advertising

  2. Jim Johnson

    Ian Collins Guest

    Jim Johnson wrote:
    > why the struct must be
    > ===================
    > typedef struct {
    > ... etc ..
    > } SetupRecord;
    >
    > CANNOT be ...
    > ===================
    > struct {
    > ... etc ..
    > } SetupRecord;
    >

    Because this does not define a struct, which I should have said more
    clearly in my other reply.

    --
    Ian Collins.
    Ian Collins, Mar 11, 2008
    #2
    1. Advertising

  3. Jim Johnson

    James Kanze Guest

    On Mar 11, 5:13 am, Ian Collins <> wrote:
    > Jim Johnson wrote:
    > > why the struct must be
    > > ===================
    > > typedef struct {
    > > ... etc ..
    > > } SetupRecord;


    > > CANNOT be ...
    > > ===================
    > > struct {
    > > ... etc ..
    > > } SetupRecord;


    > Because this does not define a struct,


    It defines an unnamed struct, and a variable named SetupRecord
    with the type of the unnamed struct. (It's rarely useful, since
    without a name, no other part of the code can refer to the
    type.)

    In C++, I wouldn't really expect to see either. The usual way
    of defining a class type in C++ is:
    struct SetupRecord { ... } ;
    No typedef needed. (For that matter, this is probably the most
    usual way of defining a structure type in C as well. But in C,
    you can't use the structure name directly as a type.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Mar 11, 2008
    #3
  4. Jim Johnson

    Guest

    On 11 Mrz., 05:11, Jim Johnson <> wrote:
    > why the struct must be
    > ===================
    > typedef struct {
    > ... etc ..
    >
    > } SetupRecord;
    >
    > CANNOT be ...
    > ===================
    > struct {
    > ... etc ..
    >
    > } SetupRecord;
    >
    > ===================
    > static const SetupRecord g_SetupRecordTable[] = {
    > { TEST1, "gggg"},
    >
    > };

    [..]

    The first version defines a new datatype "SetUpRecord". It can be used
    to declare new variables of this type and acts as a kind of shortcut
    to the long-winded struct { ... }. A type is unable to store any data
    - view it as a construction blueprint for a house. The blueprint shows
    how the building should look like and you can use it to create many
    houses that look the same. You will have troubles to put your bed into
    the plan or lock a door - you need a real building for that.

    The second version declares a new variable "SetUpRecord" that you can
    use to store data, call member functions and so on. In the practical
    analogy above, you are putting bricks together.

    You use it latere as a datatype (you declare an array where each
    element shall be of *type* SetUpRecord). Essentially you are saying,
    "build me a street of buildings, where each house should be made
    according to this plan." - This is where my blueprint example is not a
    complete analogy. In real life, you could make a house just the same
    as an existing one, replacing the blueprint by a house. In C++ you are
    forced referencing to the blueprint always.

    best,

    Michael
    , Mar 11, 2008
    #4
  5. Jim Johnson

    Jim Johnson Guest

    >typedef struct {
    > ... etc ..
    >} SetupRecord;


    above you define a type SetupRecord

    >struct {
    > ... etc ..
    >} SetupRecord;


    above you define a variable (identifier=SetupRecord) with un-named
    type.
    "It defines an unnamed struct, and a variable named SetupRecord
    with the type of the unnamed struct. (It's rarely useful, since
    without a name, no other part of the code can refer to the
    type.)"





    >why the struct must be
    >===================
    >typedef struct {
    > ... etc ..
    >} SetupRecord;
    >
    >CANNOT be ...
    >===================
    >struct {
    > ... etc ..
    >} SetupRecord;
    >
    >===================
    >static const SetupRecord g_SetupRecordTable[] = {
    > { TEST1, "gggg"},
    >};
    >
    >===================
    >1>main.cpp
    >1>d:\remote3\remote\remotedemo\app\remotedemo\main.cpp(10) : error
    >C4430: missing type specifier - int assumed. Note: C++ does not
    >support default-int
    >1>d:\remote3\remote\remotedemo\app\remotedemo\main.cpp(10) : error
    >C2146: syntax error : missing ';' before identifier
    >'g_SetupRecordTable'
    >1>d:\remote3\remote\remotedemo\app\remotedemo\main.cpp(10) : error
    >C2373: 'SetupRecord' : redefinition; different type modifiers
    >1> d:\remote3\remote\remotedemo\app\remotedemo\main.cpp(8) :
    >see declaration of 'SetupRecord'
    >1>d:\remote3\remote\remotedemo\app\remotedemo\main.cpp(10) : error
    >C4430: missing type specifier - int assumed. Note: C++ does not
    >support default-int
    >1>d:\remote3\remote\remotedemo\app\remotedemo\main.cpp(11) : error
    >C2078: too many initializers
    Jim Johnson, Mar 11, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. baobaoba

    why this code does not work

    baobaoba, Sep 17, 2003, in forum: C++
    Replies:
    3
    Views:
    364
    Noah Roberts
    Sep 18, 2003
  2. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    450
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    862
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,762
    Smokey Grindel
    Dec 2, 2006
  5. Skybuck Flying
    Replies:
    16
    Views:
    659
    tragomaskhalos
    Aug 25, 2007
Loading...

Share This Page