template -> instance -> function unresolved ???

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hello,

in the following sample i have a template class (wrapping a pointer) and a
friend template function accessing private data in the template.
i compile this into a dll (yes i know, there is no exported function from
this dll, but this is just a sample) and
get an unresolved external - the friend function is used, but not
instantiated.
what to do to get the friend function instantiated?

i tried to explicit instantiate the friend function (/DTEST2) , but this way
i get a compile error that the friend cannot access private data of the
class.
(when i do this without templates, with a concrete class and a concrete
friend function, there is no such problem )

following the sample code + compiler call.
i use cl : Microsoft (R) 32-bit C/C++ Optimizing Compiler Version
15.00.21022.08 for 80x86
from Visual Studio 2008 Express.

thanks for any help.

mario.

========= tst1.cpp =======
// simple template wrapping an "Element" pointer
// with a friend function to access the pointer.
// (this is a small part of a big complex framework - so
// please do not ask why elementForOps is a friend and not a member)

template <class Element>
class IElemPointer
{
public:

IElemPointer () : ivPtr(0) {}

friend Element& elementForOps (IElemPointer <Element>&);

protected:

private:

Element* ivPtr;
};

// here is the impl of the template friend function which
// accesses the ivPtr;
template <class Element>
Element&
elementForOps (IElemPointer <Element>& ptr)
{ return *(ptr.ivPtr);
}

// a real element class
class Foo
{
};

// the following has no effect (if commented out or not)
template IElemPointer<Foo>;

#ifdef TEST2
// the following results in a compile error in the template instance
// tst1.cpp(21) : error C2248: 'IElemPointer<Element>::ivPtr' :
// cannot access private member declared in class 'IElemPointer<Element>'
// ???? This is a friend function of the class !
template Foo & elementForOps<Foo>(IElemPointer <Foo>& ptr);
#endif

// a dummy function to call elementForOps
void bar(IElemPointer<Foo> pFoo)
{
elementForOps(pFoo);
}

// cl -W4 -nologo /LD tst1.cpp
// tst1.obj : error LNK2019: unresolved external symbol
// "class Foo & __cdecl elementForOps(class IElemPointer<class Foo> &)"
// (?elementForOps@@YAAAVFoo@@AAV?$IElemPointer@VFoo@@@@@Z)
// referenced in function "void __cdecl bar(class IElemPointer<class
Foo>)" (?bar@@YAXV?$IElemPointer@VFoo@@@@@Z)
================ EOF ==========


.



Relevant Pages

  • Re: Template friend (unary and binary) operators
    ... that with unary operator- as a friend function. ... template class MyTemplate; ... Trying to compile this wit MS Visual C++ 7.x returns me some syntax and ...
    (comp.lang.cpp)
  • Re: May a template argument be a friends of the template class?
    ... > diagnostic messages re ill-formed code, ... friend declaration of a class. ... If the identifier resolves to a typedef-name or a template ...
    (comp.lang.cpp)
  • Re: Circular Class Template Friendship
    ... > My issue is the syntax of declaring a friend class within ... > which the string is a fixed width that is specialized. ... it is still possible to have Name_Id_Table template ... the friend class declaration. ...
    (comp.lang.cpp)
  • Re: Class templates and friend function templates
    ... > It never occurred to me that the friend declaration could be an overload. ... > template ... I suspect the online version uses an EDG option to "instantiate ...
    (microsoft.public.vc.language)
  • Re: Template friendship to nested template class
    ... friend struct INNER; ... Probably you are missing an EXTRA template to declare ... I think it's because the name referred to in a 'friend' declaration is looked up to exist at the namespace scope. ...
    (microsoft.public.vc.language)