Teen Programmers Unite  
 

 

Return to forum top

template inner classes

Posted by pramod [send private reply] at February 26, 2003, 12:11:55 AM

Well, I have this class here :-

template <class T> class BigBull  {
public:

	class inside   {
	public:
		void doThings();
	};
};

template <class T> void BigBull<T>::inside::doThings()  {
	cout << "Doing things" << endl;
}

int main()  {
	BigBull<int>::inside in;
	in.doThings();
}


This code compiles correctly under G++, but MSVC [98] says external symbol doThings is not resolved. Is this a bug in template instantiation in the compiler?

Actually, I ran into this problem trying to create a binary tree collection template similar to the other collection classes. I couldn't get the iterators work. These iterators are nested classes right? How do they work? [By work, I mean the syntax, not the implementation] Does anybody have any idea about doing such things with templates? I tried to explicitly instantiate the template with something like

template class BigBull<int>::inside;


But the compiler didn't like that as inside is not a template. Is there a workaround for this?
Posted by CViper [send private reply] at February 26, 2003, 03:30:30 AM

try inserting the code directly into the class declaration

template <typename T> class Bleh
{
    public:
        class inside
        {
            void doThings() { printf( "Bleh\n" ); }
        };
};

Looks somewhat ugly, but it works for me under vc++ 6.
Posted by DragonWolf [send private reply] at February 26, 2003, 04:28:45 AM

if you don't declare your inner class as public, then it (should depending on compiler) default to private.

private means that it cannot be accessed from outside the class template. So when your second template tries to use the inner class, it cannot "see" it since it is private.

In CViper's solution he has declared the inner class public, so it can now be accessed from outside the class template.

It would suprise me if you could access doThings() using CVipers solution since that too (should) default to private.

Posted by CViper [send private reply] at February 26, 2003, 05:45:24 AM

Forgot a public, but if you check pramod's code, both the inner class and the doThings() function are declared public.

Posted by DragonWolf [send private reply] at February 26, 2003, 06:24:15 AM

oh yeah..

its the indenting that got me :P

Posted by CViper [send private reply] at February 26, 2003, 06:44:46 AM

yeah, he's using an evil way to format his code :D

Posted by DragonWolf [send private reply] at February 26, 2003, 08:49:15 AM

Why have you put semicolons after the blocks of code in your first template?

Posted by pramod [send private reply] at March 01, 2003, 12:14:16 PM

CViper's soln. was the first thing that occured to me, but that makes all the methods inline [in the inner class]. Which is not what I want. I want to decide who is, and who is not, inline.

Posted by CViper [send private reply] at March 02, 2003, 05:20:47 AM

I think you can't precomile template code in cpp-files, at least not in vc++. The compiler seems to need the actuall source code when compiling templates.

And in vc++ you never really decide what's going to be inline and what not. The inline keyword is just a hint, the compiler will decide if it's going to honor it or not (at least with the default compiler settings). And in debug mode everything is "un-inlined" anyway.

One can even tell the comiler to inline suitable functions without the inline keyword/without them beeing in the headers.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.