Skip to main content
gamedev.net
🔒 Locked

template problem

Started by Noodles 3 replies 250+ views
Original Post
Noodles
Noodles
could anyone tell me why this code: #include <iostream.h> template class A { public: class B { public: void f(); }; }; template void A::B::f() { cout << "hello\n"; } void main() { A::B b; b.f(); } generate a linking error: error LNK2001: unresolved external symbol "public: void __thiscall A::B::f(void)" (?f@B@?$A@H@@QAEXXZ) it works fine without templates, but for my projects i need some inner classes inside some of my collection classes thanks.
darookie
darookie
You should use the code tags because half of your code isn''t visible right know...
SabreMan
SabreMan
The compiler needs full visibility of the template at the point the template is instantiated. In plain English, that means put the template definition in the header.
null_pointer
null_pointer
IIRC, Microsoft Visual C++ 6.0 requires classes nested in class templates to be defined within the class template's definition, ala:


  
#include <iostream>

template <class T>
class A
{
public:
class B
{
public:
void f() { cout << "hello\n"; }
};
};

int main()
{
A<T>::B b;

b.f();
}


I don't know exactly which version of Visual C++ you are using (if any), so that may or may not solve your problem.

A couple of other problems:

1. C++ has no "void main()". Use "int main()".
2. C++ no longer has the .h prefix on its headers.


quote:
Original post by SabreMan

The compiler needs full visibility of the template at the point the template is instantiated. In plain English, that means put the template definition in the header.


I don't see how that could be a problem, seeing as his example program is all in one source file.

[edited by - null_pointer on July 21, 2002 8:15:38 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.