Solved

Cannot invoke a template member function?

Posted on 2006-07-02
9
268 Views
Last Modified: 2010-04-24
I define a template function in a class called CTestClass, as follows:

class CTestClass
{
public:
CTestClass();
virtual ~CTestClass();

public:
template<class T> void f() {
...
do something
...
};
};

Then I need to invoke the template function f, as follows:

CTestClass TestClass;

TestClass.f<int>();
^^^^^^^^^^^^

But the above statement always get a compile error:

error C2062: type 'int' unexpected.

However, if I define the template function f as a global function intead in a class, like this,

template<class T> void f() {
...
do something
...
};

then the compile will be correct.

What's wrong with this?
0
Comment
Question by:chcw
  • 3
  • 2
9 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17029058
class CTestClass
{
public:
    CTestClass(){};
    virtual ~CTestClass(){};

public:
    template<class T> void f()
    {
    };
};


int _tmain(int argc, _TCHAR* argv[])
{
    CTestClass TestClass;
    TestClass.f<int>();

    return 0;
}

This is compiled in VC++ 8.0. What version do you use?
0
 
LVL 8

Author Comment

by:chcw
ID: 17034335
I'm using Visual C++ 6.0
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
ID: 17035072
Your code is correct according to C++ standard, but VC++ 6.0 developed in 1998 doesn't support it. You need to do something else like using global function or make whole class templated.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:bastibartel
ID: 17050361
Hi there,

I have had trouble with explicite instantiation too (VC6++).
And my workaround was a dummy argument to the template function to specifiy the template type

class A
{
  public:
   A() {;}
   template <class T> void Func(T* pT)
   {
   }
};

//** then this implicite instant. will work.

  A        a;
  double d
  a.Func(&d);

Cheers,
Sebastian
0
 
LVL 5

Assisted Solution

by:bastibartel
bastibartel earned 250 total points
ID: 17055614
Hi there,
I checked back with my compiler (VC6++) and found that
explicit specialization for member functions does not compile - all others work fine.
I think I remember reading about it being a know non-conformity of VC6

class A
{
  public:
   A() {;}
   template <class T> void Func(T Val)
   {;}
};


template <class T> void Func2(T Val)
{;}


void CAaDlg::OnOK()
{
  A a;
  a.Func(3.0);                      // ok
  a.Func<double>(3.0);        // <= C2062

  Func2(2.0);                       // ok
  Func2<double>(2.0);         // ok

0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17214326
AlexFM noted correctly.
>Your code is correct according to C++ standard, but VC++ 6.0 developed in 1998 doesn't support it.

I explained that
> explicit specialization for (template) member functions does not work in VC++6, but implicite specialization can be used instead in VC++6.
  (the whole class does not neccessarily have to be templated)

That solves the problem for the Asker.
Either that or switching to VC++8.

Sebastian
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now