473,698 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with multiple parameter templates and function overloading

I have written the following very short template class in testclass.h:

template<typena me C,typename T>
class TestClass {
public:
TestClass() {};
~TestClass();
private:
T testFunction(T dummy);
};

template<typena me C,typename T>
T TestClass<C,T>: :testFunction(T dummy)
{
C bla;
return dummy;

}

So far it compilates fine.
Now i want to overload the testFunction:

template<typena me C,typename T>
class TestClass {
public:
TestClass() {};
~TestClass();
private:
T testFunction(T dummy);
/* definition of overloaded function */
double testFunction(do uble dummy);
};

template<typena me C,typename T>
T TestClass<C,T>: :testFunction(T dummy)
{
C bla;
return dummy;

}

/* now comes the problematic part */

template<typena me C>
double TestClass<C,dou ble>::testFunct ion(double dummy)
{
C bla;
return dummy;
}

I get the following compiler error:

behavior/testclass.h:47: error: no `double TestClass<C,
double>::testFu nction(double)' member function declared in class `
TestClass<C, double>'
behavior/testclass.h:47: error: template definition of non-template
`double
TestClass<C, double>::testFu nction(double)'

If I remove the parameter C everything works fine of course.
Please can anybody help me.
I think its probably just a small problem.

Sep 12 '06 #1
1 2503

ny*****@hotmail .com wrote:
I have written the following very short template class in testclass.h:

template<typena me C,typename T>
class TestClass {
public:
TestClass() {};
~TestClass();
private:
T testFunction(T dummy);
};

template<typena me C,typename T>
T TestClass<C,T>: :testFunction(T dummy)
{
C bla;
return dummy;

}

So far it compilates fine.
Now i want to overload the testFunction:

template<typena me C,typename T>
class TestClass {
public:
TestClass() {};
~TestClass();
private:
T testFunction(T dummy);
/* definition of overloaded function */
double testFunction(do uble dummy);
};

template<typena me C,typename T>
T TestClass<C,T>: :testFunction(T dummy)
{
C bla;
return dummy;

}

/* now comes the problematic part */

template<typena me C>
double TestClass<C,dou ble>::testFunct ion(double dummy)
{
C bla;
return dummy;
}
Your testFunction is not a template function. I am not quite sure what
you are trying to do, but the bottom line is you have a template class.

So, if you want to have a specialized testfunction implementation for
double, then first specialize the template class TestClass for double
and then have an implementation for that.

Look up partial specialization (function templates cannot support them)
something like this...

template<typena me C>
class TestClass<C, double>
{
public:
double testFunction(do uble dummy);
};

template<typena me C>
double TestClass<C,dou ble>::testFunct ion(double dummy)
{
C bla;
return dummy;
}
>
I get the following compiler error:

behavior/testclass.h:47: error: no `double TestClass<C,
double>::testFu nction(double)' member function declared in class `
TestClass<C, double>'
behavior/testclass.h:47: error: template definition of non-template
`double
TestClass<C, double>::testFu nction(double)'

If I remove the parameter C everything works fine of course.
Please can anybody help me.
I think its probably just a small problem.
Sep 12 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6478
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. Note that the overload is identical to the specialization except, of course, for the missing "template <>". I don't know if my questions will be a bit too broad or not, but I thought I'd give it shot... When is overloading preferable to...
7
1999
by: Hendrik Schober | last post by:
Hi, I have a problem, that boils down to the following code: #include <iostream> #include <typeinfo> class Test1 {}; class Test2 {}; class Test3 {};
4
2397
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a hierarchy of algebraic matrices with the addition operation. Thus, I want to have a virtual base class class Matr;
16
16277
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
2
1594
by: recover | last post by:
#include <stdio.h> template<class T> class TpHello { public: int GetHash(){return 0;} protected: private: T a;
1
2800
by: Senthryl | last post by:
I just moved from Visual Basic to C++ and recently finished reading my book. However, I'm trying to implement something which has led me to writing the same thing many times, which I've learned is an indicator of poor design. I have a member function called GetInput of a class called UI. The function needs to accept all of the different built-in types as the same parameter. Therefore I decided to use overloading, but this requires...
6
391
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
7
2319
by: matthias.neubauer | last post by:
I have problems understanding how overloading of function templates works. Consider first the following code without any function templates ... int foo(const char *& c) { return 0; }
6
4543
by: pauldepstein | last post by:
Let double NR( double x, double(*)(const double&) f ) be the signature of a Newton-Raphson function NR. Here, f is a function which returns a double and accepts a const double&. The aim of the game is to find a zero of this function f (the point at which f crosses the x-axis). This zero-of-f which solves our problem is the double which NR returns. It remains to explain what the "double x" represents. This is the...
0
8673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9021
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8892
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7716
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
2327
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.