Solved

Cannot invoke a template member function?

Posted on 2006-07-02
9
258 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

13 Experts available now in Live!

Get 1:1 Help Now