Re: compilation error

Discussion in 'C++' started by Victor Bazarov, Jun 27, 2003.

  1. "Jan Engelhardt" <> wrote...
    > /*
    > 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 = ...

    > return 1;
    > }


    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
     
    Victor Bazarov, Jun 27, 2003
    #1
    1. Advertising

  2. >> 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?

    > 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++.

    > 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
     
    Jan Engelhardt, Jun 27, 2003
    #2
    1. Advertising

  3. Victor Bazarov

    Rolf Magnus Guest

    Jan Engelhardt wrote:

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

    >> 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;
    };
     
    Rolf Magnus, Jun 27, 2003
    #3
    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. Vittal
    Replies:
    2
    Views:
    717
    Gordon Burditt
    Jul 8, 2003
  2. =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    Replies:
    5
    Views:
    1,306
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    May 6, 2006
  3. Gavin
    Replies:
    3
    Views:
    487
    Chris Uppal
    Mar 28, 2007
  4. C__chp
    Replies:
    4
    Views:
    512
    Puppet_Sock
    Feb 15, 2008
  5. Tassador

    const string error compilation - error C2679

    Tassador, Jul 19, 2009, in forum: C Programming
    Replies:
    0
    Views:
    626
    Tassador
    Jul 19, 2009
Loading...

Share This Page