linker error 2001/compiler error 2893(templates and friend clases involved) 
Author Message
 linker error 2001/compiler error 2893(templates and friend clases involved)

Hi

I get linker error 2001 'unresolved external' when compiling this code

void B::CallFunc()
{
 A a;
 A::Pair<int> p;
 a.Func(p); //this line here if commented out will link

Quote:
}

Following is the declaration and definitions of classes A and B. They are
setup using the default files provided when you click New Class in the
workspace.

/**************Class A**************************/
//  In A.h
class A
{
private:
 template <class T>
 class Pair
 {
  public:
   T a,b;
 };

public:
 template <class T> void Func(Pair<T> p);
 friend class B;

Quote:
};

//    In A.cpp
template <class T> void A::Func(Pair<T> p)
{

Quote:
}

/**************Class B**************************/
//  In B.h
class B
{
public:
 static void CallFunc();

Quote:
};

//  In B.cpp
void B::CallFunc()
{
 A a;
 A::Pair<int> p;
 a.Func(p);

Quote:
}

//    in main.cpp
#include "A.h"
#include "B.h"

void main()
{
 B::CallFunc();

Quote:
}

Compiling it generates this error
error LNK2001: unresolved external symbol "public: void __thiscall
A::Func(class A::Pair<int>)"

Even more interesting is if I remove A.cpp from the compile and move the
code in to to the A.h file I get a compiler error:

 error C2893: Failed to specialize function template 'void __thiscall
A::Func(class A::Pair<T>)'        With the following template arguments:
'int'

This error is not documented in MSDN library either (which is odd).

Is this a bug with the compiler/linker? Or am I missing something?

Thanks (sorry about the long post)



Fri, 25 Jun 2004 15:06:06 GMT  
 linker error 2001/compiler error 2893(templates and friend clases involved)
Why template <class T> void A::Func(Pair<T> p) ?
A is not template class
Arkady


Quote:
> Hi

> I get linker error 2001 'unresolved external' when compiling this code

> void B::CallFunc()
> {
>  A a;
>  A::Pair<int> p;
>  a.Func(p); //this line here if commented out will link
> }

> Following is the declaration and definitions of classes A and B. They are
> setup using the default files provided when you click New Class in the
> workspace.

> /**************Class A**************************/
> //  In A.h
> class A
> {
> private:
>  template <class T>
>  class Pair
>  {
>   public:
>    T a,b;
>  };

> public:
>  template <class T> void Func(Pair<T> p);
>  friend class B;
> };

> //    In A.cpp
> template <class T> void A::Func(Pair<T> p)
> {
> }

> /**************Class B**************************/
> //  In B.h
> class B
> {
> public:
>  static void CallFunc();
> };

> //  In B.cpp
> void B::CallFunc()
> {
>  A a;
>  A::Pair<int> p;
>  a.Func(p);
> }

> //    in main.cpp
> #include "A.h"
> #include "B.h"

> void main()
> {
>  B::CallFunc();
> }

> Compiling it generates this error
> error LNK2001: unresolved external symbol "public: void __thiscall
> A::Func(class A::Pair<int>)"

> Even more interesting is if I remove A.cpp from the compile and move the
> code in to to the A.h file I get a compiler error:

>  error C2893: Failed to specialize function template 'void __thiscall
> A::Func(class A::Pair<T>)'        With the following template arguments:
> 'int'

> This error is not documented in MSDN library either (which is odd).

> Is this a bug with the compiler/linker? Or am I missing something?

> Thanks (sorry about the long post)



Fri, 25 Jun 2004 16:46:12 GMT  
 linker error 2001/compiler error 2893(templates and friend clases involved)
Sorry , no problem with it and everything will work if you implement it in
*.h ( inside class ) and not in *.cpp
like
class A
{
......
public:
template <class T> void Func(Pair<T> p){/*code if needed here*/} ;
friend class B;
Quote:
};

Look at Q241949 of MSDN
HTH
Arkady


Quote:
> Why template <class T> void A::Func(Pair<T> p) ?
> A is not template class
> Arkady



> > Hi

> > I get linker error 2001 'unresolved external' when compiling this code

> > void B::CallFunc()
> > {
> >  A a;
> >  A::Pair<int> p;
> >  a.Func(p); //this line here if commented out will link
> > }

> > Following is the declaration and definitions of classes A and B. They
are
> > setup using the default files provided when you click New Class in the
> > workspace.

> > /**************Class A**************************/
> > //  In A.h
> > class A
> > {
> > private:
> >  template <class T>
> >  class Pair
> >  {
> >   public:
> >    T a,b;
> >  };

> > public:
> >  template <class T> void Func(Pair<T> p);
> >  friend class B;
> > };

> > //    In A.cpp
> > template <class T> void A::Func(Pair<T> p)
> > {
> > }

> > /**************Class B**************************/
> > //  In B.h
> > class B
> > {
> > public:
> >  static void CallFunc();
> > };

> > //  In B.cpp
> > void B::CallFunc()
> > {
> >  A a;
> >  A::Pair<int> p;
> >  a.Func(p);
> > }

> > //    in main.cpp
> > #include "A.h"
> > #include "B.h"

> > void main()
> > {
> >  B::CallFunc();
> > }

> > Compiling it generates this error
> > error LNK2001: unresolved external symbol "public: void __thiscall
> > A::Func(class A::Pair<int>)"

> > Even more interesting is if I remove A.cpp from the compile and move the
> > code in to to the A.h file I get a compiler error:

> >  error C2893: Failed to specialize function template 'void __thiscall
> > A::Func(class A::Pair<T>)'        With the following template arguments:
> > 'int'

> > This error is not documented in MSDN library either (which is odd).

> > Is this a bug with the compiler/linker? Or am I missing something?

> > Thanks (sorry about the long post)



Fri, 25 Jun 2004 17:20:26 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. internal compiler error involving templates (cross-post)

2. Linker error 2001 @&(#&@#&$@

3. Linker 2001 error

4. Linker error 2001 @&(#&@#&$@

5. linker 2001 error

6. Linker error 2001

7. Linker 2001 Error

8. linker 2001 error

9. BUG: Template Friend Function Causes Compiler Error C2248 still NOT CORRECTED in VC++6.0

10. BUG: Template Friend Function Causes Compiler Error C2248

11. quick question in atl linker error involving __IID_DEFINED__

12. Friend linker error Visual C++ .NET 2003

 

 
Powered by phpBB® Forum Software