whats wrong with this ? (compilation errors)

2

2b|!2b==?

typedef struct llist_entry_s llist_entry; /* opaque type */

struct llist_entry_s
{
llist_entry * next;
char * keyword;
char * value;

llist_entry() :next(0), keyword(0), value(0)
{
}
};
 
J

jalina

2b|!2b==? a écrit :
typedef struct llist_entry_s llist_entry; /* opaque type */

struct llist_entry_s
{
llist_entry * next;
char * keyword;
char * value;

llist_entry() :next(0), keyword(0), value(0)
{
}
};
llist_entry() is not a valid member function declaration. Did you mean
llist_entry_s() ?

J.
 
J

James Kanze

typedef struct llist_entry_s llist_entry; /* opaque type */

Why the typedef? I'd just write:

class llist_entry ;

(My coding standards say to only use the keyword "class" for
forward declarations.)
struct llist_entry_s
{
llist_entry * next;
char * keyword;
char * value;
llist_entry() :next(0), keyword(0), value(0)

There are two errors here: first, you didn't specify a return
type for the function, and second, you try to use an initializer
sequence which is only legal in constructors.
 
M

Mike Wahler

2b|!2b==? said:
typedef struct llist_entry_s llist_entry; /* opaque type */

struct llist_entry_s
{
llist_entry * next;
char * keyword;
char * value;

llist_entry() :next(0), keyword(0), value(0)
{
}
};

You don't say what compiler errors you get.

The above code compiles successfully for me with VC++.

BTW you don't need that typedef. You could write it
like this:

struct llist_entry
{
llist_entry * next;
char * keyword;
char * value;

llist_entry() :next(0), keyword(0), value(0)
{
}
};

-Mike
 
M

Mike Wahler

Why the typedef? I'd just write:

class llist_entry ;

(My coding standards say to only use the keyword "class" for
forward declarations.)
There are two errors here: first, you didn't specify a return
type for the function,

It's a constructor; no return type.
and second, you try to use an initializer
sequence which is only legal in constructors.

It is a ctor.

-Mike
 
2

2b|!2b==?

Mike said:
You don't say what compiler errors you get.

The above code compiles successfully for me with VC++.

BTW you don't need that typedef. You could write it
like this:

struct llist_entry
{
llist_entry * next;
char * keyword;
char * value;

llist_entry() :next(0), keyword(0), value(0)
{
}
};

-Mike
Ah, I'm back now - I needed the break (and some coffee). I sorted this
one out. It was due to the file extension (*.c) - it was expecting C
code - so compiler barfed when it got to the initialization list ...
 
H

Heinz Ozwirk

Mike Wahler said:
It's a constructor; no return type.

A constructor is a "function" which has the same name as the class (or
struct) it belongs to. Here, the name of the struct is "llist_entry_s" and
the name of the supposed c-tor is "llist_entry". Those names are different
and therefore llist_entry_s::llist_entry() is not a constructor. It doesn't
matter that somewhere else there is a typedef for llist_entry. Any names
declared outside the scope of llist_entry_s may be overwritten by another
declaration inside that scope.

Heinz
 
J

James Kanze

You don't say what compiler errors you get.
The above code compiles successfully for me with VC++.

It shouldn't. G++ says:
ctor.cc:16: error: ISO C++ forbids declaration of 'llist_entry'
with no type
ctor.cc:18: error: declaration of 'int
llist_entry_s::llist_entry()'
ctor.cc:8: error: changes meaning of 'llist_entry' from 'typedef
struct llist_entry_s llist_entry'
ctor.cc: In member function 'int llist_entry_s::llist_entry()':
ctor.cc:16: error: only constructors take base initializers
Which is what I'd expect.
BTW you don't need that typedef. You could write it
like this:
struct llist_entry
{
llist_entry * next;
char * keyword;
char * value;
llist_entry() :next(0), keyword(0), value(0)
{
}
};

That would be more idiomatic C++, of course.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top