why this code not work

J

Jim Johnson

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
 
I

Ian Collins

Jim said:
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.
 
J

James Kanze

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.)
 
M

Michael.Boehnisch

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
 
J

Jim Johnson

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top