Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cannot invoke a template member function?

Posted on 2006-07-02
9
Medium Priority
?
309 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 1000 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: 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.

 
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 1000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

730 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