Solved

Cannot invoke a template member function?

Posted on 2006-07-02
9
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create a progress bar for timer 4 303
memorymap in visual studio 3 72
.Net Errors Service Crashing 5 158
C Language combined operators 28 137
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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