Topic: stl list with VC++5


Author: "J.Barfurth" <techview@bfk-net.de>
Date: 1999/12/01
Raw View

Gilles ROUX <gilles.roux@insa-lyon.fr> schrieb in im Newsbeitrag:
3840B365.646F5247@insa-lyon.fr...
> Hi,
> The following code compiles perfectly with gcc, but not with VC++5
(see
> error message).
> Have I made a silly mistake, or is vc++ wrong (once more). Is it going
> to work if I use vc++6 ?

No. The STL hasn't changed there.

> If not, how can I solve this problem ?

According to the standard you must not instantiate a standard library
template with an incomplete type.

Rethink your design. If you really need such an iterator within A here
is a workaround:

> --------------------------------------
> #include <list>
>
> class A
> {
> public:
>   A();
>  // std::list<A>::iterator a;

<WORKAROUND>
    // need these now:
    A(A const&);
    A& operator=(A const&);
    ~A();

    // here is it
    struct ListIterator;
    ListIterator* a;
};

struct A::ListIterator
{
    std::list<A>::iterator value;    // ok here

    ListIterator() {}
    ListIterator(std::list<A>::iterator const& it) : value(it) {}
};
A::A() : a(new ListIterator()) {}    // or just NULL ?
A::~A() { delete a; }

// copy construction and assignment left as an exercise ;-)
</WORKAROUND>

BTW:
> void main()
> {
> }
    int main() {}
main() is required to have a return type of int in C++ (and C).
If your compiler doesn't like it without, add 'return 0;' to the body.

-- J   rg Barfurth



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Gilles ROUX <gilles.roux@insa-lyon.fr>
Date: 1999/11/30
Raw View
Hi,
The following code compiles perfectly with gcc, but not with VC++5 (see
error message).
Have I made a silly mistake, or is vc++ wrong (once more). Is it going
to work if I use vc++6 ?
If not, how can I solve this problem ?
Thanks.
Gilles.

--------------------------------------
#include <list>

class A
{
public:
  A();
  std::list<A>::iterator a;
};

void main()
{
}
----------------------------------------
d:\Prog\DevStudio\VC\INCLUDE\list(24) : error C2079: '_Value' uses
undefined class 'A'
d:\Prog\DevStudio\VC\INCLUDE\functional(180) : error C2079: 'value' uses
undefined class 'A'
d:\Prog\DevStudio\VC\INCLUDE\list(173) : error C2440: 'default argument'
: cannot convert from 'class A' to 'class A'
                                                      Source or target
has incomplete type
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Michiel Salters <salters@lucent.com>
Date: 1999/12/01
Raw View
Gilles ROUX wrote:
>
> Hi,
> The following code compiles perfectly with gcc, but not with VC++5 (see
> error message).
> Have I made a silly mistake, or is vc++ wrong (once more). Is it going
> to work if I use vc++6 ?
> If not, how can I solve this problem ?
> Thanks.
> Gilles.

> --------------------------------------
> #include <list>

> class A
> {
> public:
>   A();
>   std::list<A>::iterator a;
> };

> void main()
> {
> }
> ----------------------------------------
> d:\Prog\DevStudio\VC\INCLUDE\list(24) : error C2079: '_Value' uses
> undefined class 'A'
> d:\Prog\DevStudio\VC\INCLUDE\functional(180) : error C2079: 'value' uses
> undefined class 'A'
> d:\Prog\DevStudio\VC\INCLUDE\list(173) : error C2440: 'default argument'
> : cannot convert from 'class A' to 'class A'
>                                                       Source or target
> has incomplete type
> ---

You made a silly mistake - but that's void main().

The not so silly mistake that (probably :-) causes the compiler
trip-up is the use of std::list<A> inside class A. What if e.g.
the size of std::list<A> depends on the size of A? Not unreasonable,
but in that case you can't put an std::list<A> inside an A.

The basic rule is that you can't use class A as a template parameter
(to std::list, that is) until after the final '}' of its definition.
It isn't complete yet; hence the last warning. Using A* is of course
possible (its size does not depend on the sizeof(A) ). This might give
you the intended behavior. I'm wondering why an A object has to hold
an iterator to the list, though. In one way, that makes class A like
a linked list node, which is quite confusing, since it is (presumably)
on another (std::)list, too.

On the compiler comparison.
gcc is as right as vc++; this behavior is undefined behavior. Accepting
it without diagnostic is fine; refusing it is fine too. The only thing
not fine is using A as you did.

Michiel Salters
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]