Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why this code not work

Reply
Thread Tools

why this code not work

 
 
Jim Johnson
Guest
Posts: n/a
 
      03-11-2008
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.c pp( :
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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-11-2008
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.
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      03-11-2008
On Mar 11, 5:13 am, Ian Collins <ian-n...@hotmail.com> 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
 
Reply With Quote
 
Michael.Boehnisch@gmail.com
Guest
Posts: n/a
 
      03-11-2008
On 11 Mrz., 05:11, Jim Johnson <aopiyy...@yahoo.com> 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
 
Reply With Quote
 
Jim Johnson
Guest
Posts: n/a
 
      03-11-2008
>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\mai n.cpp(10) : error
>C4430: missing type specifier - int assumed. Note: C++ does not
>support default-int
>1>d:\remote3\remote\remotedemo\app\remotedemo\mai n.cpp(10) : error
>C2146: syntax error : missing ';' before identifier
>'g_SetupRecordTable'
>1>d:\remote3\remote\remotedemo\app\remotedemo\mai n.cpp(10) : error
>C2373: 'SetupRecord' : redefinition; different type modifiers
>1> d:\remote3\remote\remotedemo\app\remotedemo\main.c pp( :
>see declaration of 'SetupRecord'
>1>d:\remote3\remote\remotedemo\app\remotedemo\mai n.cpp(10) : error
>C4430: missing type specifier - int assumed. Note: C++ does not
>support default-int
>1>d:\remote3\remote\remotedemo\app\remotedemo\mai n.cpp(11) : error
>C2078: too many initializers

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why :: ? Why not : ? Why not . ? <- less clutter ?!? Skybuck Flying C++ 16 08-25-2007 09:48 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM
why this code does not work baobaoba C++ 3 09-18-2003 02:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57