 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Victor Bazarov Guest
|
Posted: Fri Jun 27, 2003 3:24 pm Post subject: Re: compilation error |
|
|
"Jan Engelhardt" <jengelh (AT) linux01 (DOT) gwdg.de> wrote...
| Quote: | /*
Hi,
if the following program is compiled with g++, an error is issued:
dumb.cpp: In function `int main()':
dumb.cpp:9: error: cannot convert `linked_list::linked_obj*' to
`linked_obj*'
in initialization
however, TCPP (Turbo C++ DOS 16bit) did not complain.
It looks like g++ just "kills" the keyword struct (and
makes it a class), which is not what I want.
Is this a g++ bug, or g++'s intention?
If it is intention, how can it be circumvented?
I am using "g++ (GCC) 3.3 20030226 (prerelease) (SuSE Linux)"
- Jan Engelhardt
*/
struct linked_list {
struct linked_obj { int n; } obj;
};
class NiceClass { public: struct linked_list *arg; };
int main(void) {
NiceClass sth;
sth.arg = new struct linked_list;
struct linked_obj *p = &sth.arg->obj;
|
The name 'linked_obj' is unknown at this scope. By adding 'struct'
in front of it you claim that it's a struct, but it is incomplete
type (its contents are unknown). You probably meant to write
linked_list::linked_obj *p = ...
This should be a good lesson not to throw too many "struct"s around.
Your code, rid of extraneous 'struct', should look like this:
struct linked_list {
struct linked_obj { int n; } obj;
};
class NiceClass { public: linked_list *arg; };
int main(void) {
NiceClass sth;
sth.arg = new linked_list;
linked_list::linked_obj *p = &sth.arg->obj;
return 1;
}
Only use things where they are absolutely necessary.
HTH
Victor
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Fri Jun 27, 2003 3:24 pm Post subject: Re: compilation error |
|
|
Jan Engelhardt wrote:
| Quote: | /*
Hi,
if the following program is compiled with g++, an error is issued:
dumb.cpp: In function `int main()':
dumb.cpp:9: error: cannot convert `linked_list::linked_obj*' to
`linked_obj*'
in initialization
however, TCPP (Turbo C++ DOS 16bit) did not complain.
It looks like g++ just "kills" the keyword struct (and
makes it a class), which is not what I want.
Is this a g++ bug, or g++'s intention?
|
It's intention, since there is (except from the default access rights)
no difference between a struct and a class in C++. You seem to assume
something else.
| Quote: | If it is intention, how can it be circumvented?
|
Use another language than C++. Btw: why do you think that language
designer's and compiler writer's intentions always have to be
circumvented? You should first be asking yourself what you are doing
wrong, not how to circumvent language features.
| Quote: | I am using "g++ (GCC) 3.3 20030226 (prerelease) (SuSE Linux)"
- Jan Engelhardt
*/
struct linked_list {
struct linked_obj { int n; } obj;
};
class NiceClass { public: struct linked_list *arg; };
int main(void) {
NiceClass sth;
sth.arg = new struct linked_list;
struct linked_obj *p = &sth.arg->obj;
|
The above two uses of the struct keyword are superfluous. You can leave
them out. But that's not the problem. The problem is that the
linked_obj that is defined above is nested withing linked_list. So you
have to write:
linked_list::linked_obj *p = &sth.arg->obj;
|
|
| Back to top |
|
 |
Jan Engelhardt Guest
|
Posted: Fri Jun 27, 2003 3:31 pm Post subject: Re: compilation error |
|
|
| Quote: | struct linked_list {
struct linked_obj { int n; } obj;
};
class NiceClass { public: struct linked_list *arg; };
int main(void) {
NiceClass sth;
sth.arg = new struct linked_list;
struct linked_obj *p = &sth.arg->obj;
The name 'linked_obj' is unknown at this scope. By adding 'struct'
in front of it you claim that it's a struct, but it is incomplete
type (its contents are unknown). You probably meant to write
|
Hm, I defined all I need for a struct, did not I?
| Quote: | linked_list::linked_obj *p = ...
This should be a good lesson not to throw too many "struct"s around.
Your code, rid of extraneous 'struct', should look like this:
|
The "problem" is, that the struct and its processing functions (obviously
linked lists) is in the "C" scope (extern "C"). That is, the header and the
library for LLs are C, whereas the program I intend to write is C++.
| Quote: | struct linked_list {
struct linked_obj { int n; } obj;
};
class NiceClass { public: linked_list *arg; };
int main(void) {
NiceClass sth;
sth.arg = new linked_list;
linked_list::linked_obj *p = &sth.arg->obj;
return 1;
}
Only use things where they are absolutely necessary.
HTH
|
--
- Jan Engelhardt
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Fri Jun 27, 2003 3:36 pm Post subject: Re: compilation error |
|
|
Jan Engelhardt wrote:
| Quote: | struct linked_list {
struct linked_obj { int n; } obj;
};
class NiceClass { public: struct linked_list *arg; };
int main(void) {
NiceClass sth;
sth.arg = new struct linked_list;
struct linked_obj *p = &sth.arg->obj;
The name 'linked_obj' is unknown at this scope. By adding 'struct'
in front of it you claim that it's a struct, but it is incomplete
type (its contents are unknown). You probably meant to write
Hm, I defined all I need for a struct, did not I?
|
Yes, but not in the scope you're trying to use it in.
| Quote: | linked_list::linked_obj *p = ...
This should be a good lesson not to throw too many "struct"s around.
Your code, rid of extraneous 'struct', should look like this:
The "problem" is, that the struct and its processing functions
(obviously linked lists) is in the "C" scope (extern "C"). That is,
the header and the library for LLs are C, whereas the program I intend
to write is C++.
|
Then don't define linked_obj within linked_list, but rather outside of
it:
struct linked_obj { int n; };
struct linked_list {
linked_obj obj;
};
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|