Advertisement

more msvc++ trivia

Started by April 12, 2001 04:14 PM
10 comments, last by Succinct 23 years, 10 months ago
u guys know why know why nested classes in a template class might come up unresolved? I''ve qualified the definitions exactly the same way as i would a non-template class (which works). also, the rest of the class compiles fine, only difference between the non-nested class and the nested class is the nested class'' qualifier.... any ideas/similar experience? Thank you for your bandwidth. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~Succinct Demos Online~ "&ltDiLLiGaS> I''m suprised nobody takes M$ to court for rape... The OS keeps going down on you w/o your permission."
-- Succinct(Don't listen to me)
You trying using a couple of typedefs? Once-in-a-while I run into something that I can''t get to work without them.

Typedef the template<>, and then make another typedef with for the nested class. Just to see if it works then.

Get SP5 too - I assume you already have sp3...

Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
ok, so, say i have a class template called MainClass containing a nested class called NestedClass:

  template<class T> class MainClass{public:    MainClass( void );    void MainMethod( void );    class NestedClass    {    public:        NestedClass( void );        void MainMethod( void );                };};  


i know how to define the MainClass constructor and the MainMethod, but i can''t define any of the NestedClass methods. I saw an example of doing the typedef thing for defining nested methods, but i''m not quite sure how to do this w/ the template.

something like this? (btw, as i''m replying here, i do not have access to a compiler to test this
typdef template MainClass::NestedClass MainNestedClass;
or
template typdef MainClass::NestedClass MainNestedClass;

MainNestedClass::NestedClass( void ) {} // constructor

hmmm, looks kinda funny to me, but i''ll test it tomorrow. any ideas, mag or anyone? also, i''m not sure if i''ll be able to d/l the sp''s because i generally work in public labs, and do not work on my own computer... is it possible to install the sp''s on an NT machine where i don''t have a proper privilege level?

thank you guys for your time - Succinct
-- Succinct(Don't listen to me)
You can''t typedef a class template, only an instantiation of it. In other words, you can only typedef classes, not class templates, so that workaround won''t work.

MSVC simply has trouble working with member function templates and any classes nested within template classes, unless you define them when you declare them. Define the nested class''s members entirely within the nested class definition, and it will compile correctly.
very true, null_pointer
that's what i did at work today after finding this on the MSDN:
_______________________________________________________________
BUG: LNK2001 on Member Function When Using Nested Class Template
ID: Q128789

SYMPTOMS
When you build a program that uses nested class templates, the following link error is generated:

test.obj: error LNK2001: unresolved external symbol "? Funtion@BB@?$AA@HH@@QAEHXZ( public: int __thiscall AA::BB::Funtion(void) )"

CAUSE
The compiler does not generate code for the member function in the nested class template.

RESOLUTION
To work around the problem, use one of the following suggestions:

Define the function in the class declaration as an inline function. Be sure to define the function body in the class declaration. Defining the function as an inline function outside the class declaration will not eliminate the problem.

-or-

Use the member function specialization technique to work around the problem. This technique is demonstrated in the "Sample Code" section of this article.

STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.

MORE INFORMATION
The following code can be used to reproduce or work around the problem.

Sample Code

       #include <stdio.h>   template <class T1, class T2>   class AA   {     public:       class BB       {         public:           int Function();   // Work around #1, replace the previous line with   //        int Function() { return 0; }       };   };   // Work around #2, uncomment the following function   // AA<int,int>::BB::Function()   // {   //   return 0;   // }   template <class T1, class T2>   int AA<T1,T2>::BB::Function()   {      return 0;   }   void main()   {     AA<int,int>::BB b;     printf( "%d\n", b.Function() );   }   


Last Reviewed: February 4, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.
________________________________________________________________
this time it really is a bug in msvc...
wierd, though, because this article was listed in the Knowledge Base under 5.0, and they still haven't corrected it since SP 3 of 6.0?
arrgh!

anyway, for all of you using class templates with nested classes:


Edited by - Succinct on April 13, 2001 9:27:30 PM
-- Succinct(Don't listen to me)
Hmm...interesting. I didn''t know the second workaround was possible - I guess it just forces the compiler to generate code...
Advertisement
heh, problem is, that specilization only generates code for the specialized type... I figured that if you defined on specialization, it would "trick" the compiler into realizing that it has to generate it for all of the parameterized types, too.... no dice.

so, i''m glad the class is very small and has mainly single line method definitions. It really sucks for debugging,though, when you squeeze more than one line of code onto a line and you try to peek at the variable''s contents...

oh well. thank you guys for your interest and help, and THANK YOU MICROSOFT!
-- Succinct(Don't listen to me)
The STL does this all the time - iterators are nested classes

Or is it not the same thing somehow?

Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
you know, mag, i thought of that too, and was pretty mad, thinking, i can''t do it, why can they?

i looked later and found that all of the STL ( at least) iterators are also defined in class, as per workaround #1...

arrgh

it''s hard to tell, though, because of the slowhead who actually implemented Microsoft''s STL. sure, good, solid code, you can even step through it, but I''d feel better stepping through the assembly equivalent.

that stuff''s sooooo hard to read. Is it bad coding, or professional coding? you be the judge!

-- Succinct(Don't listen to me)
pos

Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement