Topic: Nested classes, and vectors


Author: "Nat Pryce" <np2@doc.ic.ac.uk>
Date: 1997/08/08
Raw View
Roger Tragin <roger_tragin@bigfoot.com> wrote in article
<33E66B05.F3981D07@bigfoot.com>...
> This piece of code..
>
  [class definition elided]

> int main()
> {
>         vector<MyClass::NestedClass> nestdVec;
>         return 0;
> }
>
> produces [elided] compile time error with MSVC++ ver 5.0, NT4.0
>
> Why???   I do not what to use namespaces, since the code has to work
> with another compiler that does not have namespaces.


Because MSVC5 does not handle the name lookup correctly for template
arguments.

What I've managed to figure out is that MSVC uses the last name of
the full type as the template parameter to the a template -- i.e:
it thinks you are instantiating a type std::vector<NestedClass> -- and
then looks up that name in the same namespace as the template itself
is defined. I.e: it first looks for std::NestedClass and then looks
for ::NestedClass, as per usual lookup rules.

Therefore, you can work round this problem by creating a typedef in the
global namespace of the form:

typedef NestedClass MyClass::NestedClass.

However, that's not always possible, and I have found that it sometimes
causes the compiler to go into an infinite loop (well I guess it's infinite;
I'm not going to wait and see :-) and hog the CPU.

So, another workaround is to define your nested class in the global scope.
E.g:

class NestedClass { ... };

class MyClass {
...
   std::vector<NestedClass> _vec;
};


Cheers,
 Nat.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Roger Tragin <roger_tragin@bigfoot.com>
Date: 1997/08/05
Raw View
Hi,

This piece of code..

#include <vector>
using namespace std;

class MyClass {
public:

  class NestedClass {
  public:               // public methods
    // constructors
    NestedClass();
    // destructor
    ~NestedClass();

    bool operator==(const NestedClass& rhs) const;
    bool operator<(const NestedClass& rhs) const;

  protected:                    // protected methods

  private:                      // private methods

  private:                      // private data members
  };
};

int main()
{
        vector<MyClass::NestedClass> nestdVec;
        return 0;
}

produces this compile time error with MSVC++ ver 5.0, NT4.0

Compiling...
test.cpp
C:\Program Files\DevStudio\VC\INCLUDE\vector(103) : error C2065:
'NestedClass' : undeclared identifier
C:\Program Files\DevStudio\VC\INCLUDE\vector(103) : error C2440:
'default argument' : cannot convert from 'int' to 'const class
MyClass::NestedClass &'
Reason: cannot convert from 'int' to 'const class MyClass::NestedClass'
No constructor could take the source type, or constructor overload
resolution was ambiguous
Error executing cl.exe.

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

Why???   I do not what to use namespaces, since the code has to work
with another compiler that does not have namespaces.

Any Ideas??

Regards
Roger

--
 Roger Tragin                   email:  roger_tragin@bigfoot.com
 Phone: +61 3 9563 4110         web:    http://www.cst.com.au/~roger

Of all the things I ever lost, I miss my mind the most.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]