Container of class defined within another class 
Author Message
 Container of class defined within another class

Hello.

I'm porting some C++ code from Solaris to NT, using MSVC++ 5.0 w/o SP3.
It seems that MS's implementation of STL does not support container of a
class defined within another class. For example,

#include <vector>

using namespace std;

class X
{
public:
    class Y
    {
        int i;
    public:
        Y(): i(0) {}
    };

    vector<Y> vy;

Quote:
};

which results in the following compilation error:
--------------------Configuration: Study - Win32
Debug--------------------
Compiling...
Study.cpp
C:\Program Files\DevStudio\VC\INCLUDE\vector(103) : error C2065: 'Y' :
undeclared identifier
C:\Program Files\DevStudio\VC\INCLUDE\vector(103) : error C2440:
'default argument' : cannot  convert from 'int' to 'const class X::Y &'
Reason: cannot convert from 'int' to 'const class X::Y'
No constructor could take the source type, or constructor overload
resolution was ambiguous
Error executing cl.exe.

Study.obj - 2 error(s), 0 warning(s)

But the same code works fine with HP/SGI's STL.
Does SP3 fix the problem? Or can anyone supply a solution? Thanks a lot.

-Jack

FYI, the source code of <vector> where the error happens is as follows:
template<class _Ty, class _A = allocator<_Ty> >
    class vector {
public:
    ...
    void resize(size_type _N, const _Ty& _X = _Ty())
                                              ^^^^^
    {if (size() < _N)
        insert(end(), _N - size(), _X);
     else if (_N < size())
             erase(begin() + _N, end()); }

My guess is the default argument _X = _Ty() is the problem.



Fri, 13 Oct 2000 03:00:00 GMT  
 Container of class defined within another class

Hi,

This is a bug in MSVC++.  I reported it to Microsoft a while ago, but I
haven't heard anything back from them (surprise, surprise).  A similar thing
will happen with namespaces, but you can work around that with "using
namespace blah;"

I'm using SP1, I don't think that SP3 fixes it though.  I'd like to be
wrong.

Regards,

Jan Mikkelsen

Quote:

>Hello.

>I'm porting some C++ code from Solaris to NT, using MSVC++ 5.0 w/o SP3.
>It seems that MS's implementation of STL does not support container of a
>class defined within another class. For example,

>#include <vector>

>using namespace std;

>class X
>{
>public:
>    class Y
>    {
>        int i;
>    public:
>        Y(): i(0) {}
>    };

>    vector<Y> vy;
>};

>which results in the following compilation error:
>--------------------Configuration: Study - Win32
>Debug--------------------
>Compiling...
>Study.cpp
>C:\Program Files\DevStudio\VC\INCLUDE\vector(103) : error C2065: 'Y' :
>undeclared identifier
>C:\Program Files\DevStudio\VC\INCLUDE\vector(103) : error C2440:
>'default argument' : cannot  convert from 'int' to 'const class X::Y &'
>Reason: cannot convert from 'int' to 'const class X::Y'
>No constructor could take the source type, or constructor overload
>resolution was ambiguous
>Error executing cl.exe.

>Study.obj - 2 error(s), 0 warning(s)

>But the same code works fine with HP/SGI's STL.
>Does SP3 fix the problem? Or can anyone supply a solution? Thanks a lot.

>-Jack

>FYI, the source code of <vector> where the error happens is as follows:
>template<class _Ty, class _A = allocator<_Ty> >
>    class vector {
>public:
>    ...
>    void resize(size_type _N, const _Ty& _X = _Ty())
>                                              ^^^^^
>    {if (size() < _N)
>        insert(end(), _N - size(), _X);
>     else if (_N < size())
>             erase(begin() + _N, end()); }

>My guess is the default argument _X = _Ty() is the problem.



Sat, 14 Oct 2000 03:00:00 GMT  
 Container of class defined within another class

I have VC5 with SP3 and it looks like it compiles just fine. I copy and
pasted the code you have given and it compiled without any complaints. It
looks like it has been fixed in SP3. Try it out.

--
Girish Bharadwaj  [VC++/MVP]
Please don't send email queries.Post them here.
MS Knowledge base articles : http://support.microsoft.com/support

|Hi,
|
|This is a bug in MSVC++.  I reported it to Microsoft a while ago, but I
|haven't heard anything back from them (surprise, surprise).  A similar
thing
|will happen with namespaces, but you can work around that with "using
|namespace blah;"
|
|I'm using SP1, I don't think that SP3 fixes it though.  I'd like to be
|wrong.
|
|Regards,
|
|Jan Mikkelsen

|
|
|>Hello.
|>
|>I'm porting some C++ code from Solaris to NT, using MSVC++ 5.0 w/o SP3.
|>It seems that MS's implementation of STL does not support container of a
|>class defined within another class. For example,
|>
|>#include <vector>
|>
|>using namespace std;
|>
|>class X
|>{
|>public:
|>    class Y
|>    {
|>        int i;
|>    public:
|>        Y(): i(0) {}
|>    };
|>
|>    vector<Y> vy;
|>};
|>
|>which results in the following compilation error:
|>--------------------Configuration: Study - Win32
|>Debug--------------------
|>Compiling...
|>Study.cpp
|>C:\Program Files\DevStudio\VC\INCLUDE\vector(103) : error C2065: 'Y' :
|>undeclared identifier
|>C:\Program Files\DevStudio\VC\INCLUDE\vector(103) : error C2440:
|>'default argument' : cannot  convert from 'int' to 'const class X::Y &'
|>Reason: cannot convert from 'int' to 'const class X::Y'
|>No constructor could take the source type, or constructor overload
|>resolution was ambiguous
|>Error executing cl.exe.
|>
|>Study.obj - 2 error(s), 0 warning(s)
|>
|>But the same code works fine with HP/SGI's STL.
|>Does SP3 fix the problem? Or can anyone supply a solution? Thanks a lot.
|>
|>-Jack
|>
|>FYI, the source code of <vector> where the error happens is as follows:
|>template<class _Ty, class _A = allocator<_Ty> >
|>    class vector {
|>public:
|>    ...
|>    void resize(size_type _N, const _Ty& _X = _Ty())
|>                                              ^^^^^
|>    {if (size() < _N)
|>        insert(end(), _N - size(), _X);
|>     else if (_N < size())
|>             erase(begin() + _N, end()); }
|>
|>My guess is the default argument _X = _Ty() is the problem.
|>
|
|



Sat, 14 Oct 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Container of class defined within another class

2. Multiply defined symbols in VS 7.0 with derived class from templated container

3. Multiply defined symbols in VS 7.0 with derived class from templated container

4. Classes within classes

5. friend template class within template class

6. Help: Trouble calling object classes within other object classes

7. Calling View class from User define class

8. How to use a user defined class as base class

9. how to call func defined in class A from class B

10. Can I define classes w/o Class Wizard...

11. Class in class OR class derived from class?

12. Simple question: Can one class define another class within it?

 

 
Powered by phpBB® Forum Software