473,605 Members | 2,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

?explicit instantiation member function of class template

i want to explicit instantiate a member function of a class template,
and got some error,
can anyone give some advice to correct it?
3x

complier: g++ 3.2

#include <iostream>
#include <string>
using namespace std;

template <class BT> class FilterRangeItem {
public:
bool m_bIsSetLow;
bool m_bIsSetHigh;
bool m_bIsFailed;
BT m_low;
BT m_high;
FilterRangeItem ();
virtual BT _Convert(string );
virtual bool SetFilter(strin g,string);
};

template <class BT>
FilterRangeItem <BT>::FilterRan geItem() {
m_bIsSetLow = m_bIsSetHigh = m_bIsFailed = false;
}

template <class BT>
BT FilterRangeItem <BT>::_Convert( string str) {
BT a;
cout <<"FilterRAngeI tem<BT>::_Conve rt()"<<endl;
return a;
}

template <class BT>
bool FilterRangeItem <BT>::SetFilter (string low, string high) {
if (low.length()>0 ) {
m_low = _Convert(low);
if (!m_bIsFailed) m_bIsSetLow = true;
}
if (high.length() > 0) {
m_high = _Convert(high);
if (!m_bIsFailed) m_bIsSetHigh = true;
}
cout <<"SetFilter finished"<<endl ;
return m_bIsSetFailed;
}

template int FilterRangeItem <int>::_Convert (string str) {
cout <<"FilterRangeI tem<int>::_Conv ert"<<endl;
return 0;
}
template double FilterRangeItem <double>::_Conv ert(string str) {
cout <<"FilterRangeI tem<double>::_C onvert"<<endl;
return 0.0
}

class Filter {
public:
FilterRangeItem <int> iFlt;
FilterRangeItem <double> dFlt;
};

int main(int argc, char* argv[])
{
{
Filter f;
iFlt.SetFilter( "3", "5");
dFlt.SetFilter( "6.0", "9.10");
}
int x;
cin>>x;

return 0;
}

compiler error:
t1.cpp:43: parse error before `{' token
t1.cpp:47: parse error before `{' token
t1.cpp: In function `int main(int, char**)':
t1.cpp:62: `iFlt' undeclared (first use this function)
t1.cpp:62: (Each undeclared identifier is reported only once for each
function
it appears in.)
t1.cpp:63: `dFlt' undeclared (first use this function)

Dec 4 '05 #1
5 2647

he****@gmail.co m wrote:
i want to explicit instantiate a member function of a class template,
and got some error,
can anyone give some advice to correct it?
Best advice is to understand the compiler-given errors and fix them one
by one.
complier: g++ 3.2

#include <iostream>
#include <string>
using namespace std;

template <class BT> class FilterRangeItem {
public:
bool m_bIsSetLow;
bool m_bIsSetHigh;
bool m_bIsFailed;
BT m_low;
BT m_high;
It is a *very_bad* idea to declare your data members all public.
FilterRangeItem ();
virtual BT _Convert(string );
Avoid using names that start with an underscore, they are reserved for
implementation.
virtual bool SetFilter(strin g,string);
};
If you expect the class to be polymorphic (as indicated by virtual
functions), always define a virtual destructor.


template <class BT>
FilterRangeItem <BT>::FilterRan geItem() {
m_bIsSetLow = m_bIsSetHigh = m_bIsFailed = false;
}
Use constructor-initialization list instead of assignments in the body
of the constructor.

[...snip...]
template int FilterRangeItem <int>::_Convert (string str) {
cout <<"FilterRangeI tem<int>::_Conv ert"<<endl;
return 0;
This is a complete specialization of the member template. Hence you
need to introduce empty angle-brackets after the word template.

template<> int FilterRangeItem <int>::_Convert (string str) {
}
template double FilterRangeItem <double>::_Conv ert(string str) {
cout <<"FilterRangeI tem<double>::_C onvert"<<endl;
return 0.0
Same here.
}

class Filter {
public:
FilterRangeItem <int> iFlt;
FilterRangeItem <double> dFlt;
};

int main(int argc, char* argv[])
{
{
Filter f;
iFlt.SetFilter( "3", "5");
Whats that? C++ needs variables to be declared before they are used.
dFlt.SetFilter( "6.0", "9.10");


Same.

Hope this helps.

Dec 4 '05 #2
specialization is done as, in VS.NET otherwise works....

template<> int f(......)
he****@gmail.co m wrote:
i want to explicit instantiate a member function of a class template,
and got some error,
can anyone give some advice to correct it?
3x

complier: g++ 3.2

#include <iostream>
#include <string>
using namespace std;

template <class BT> class FilterRangeItem {
public:
bool m_bIsSetLow;
bool m_bIsSetHigh;
bool m_bIsFailed;
BT m_low;
BT m_high;
FilterRangeItem ();
virtual BT _Convert(string );
virtual bool SetFilter(strin g,string);
};

template <class BT>
FilterRangeItem <BT>::FilterRan geItem() {
m_bIsSetLow = m_bIsSetHigh = m_bIsFailed = false;
}

template <class BT>
BT FilterRangeItem <BT>::_Convert( string str) {
BT a;
cout <<"FilterRAngeI tem<BT>::_Conve rt()"<<endl;
return a;
}

template <class BT>
bool FilterRangeItem <BT>::SetFilter (string low, string high) {
if (low.length()>0 ) {
m_low = _Convert(low);
if (!m_bIsFailed) m_bIsSetLow = true;
}
if (high.length() > 0) {
m_high = _Convert(high);
if (!m_bIsFailed) m_bIsSetHigh = true;
}
cout <<"SetFilter finished"<<endl ;
return m_bIsSetFailed;
}

template int FilterRangeItem <int>::_Convert (string str) {
cout <<"FilterRangeI tem<int>::_Conv ert"<<endl;
return 0;
}
template double FilterRangeItem <double>::_Conv ert(string str) {
cout <<"FilterRangeI tem<double>::_C onvert"<<endl;
return 0.0
}

class Filter {
public:
FilterRangeItem <int> iFlt;
FilterRangeItem <double> dFlt;
};

int main(int argc, char* argv[])
{
{
Filter f;
iFlt.SetFilter( "3", "5");
dFlt.SetFilter( "6.0", "9.10");
}
int x;
cin>>x;

return 0;
}

compiler error:
t1.cpp:43: parse error before `{' token
t1.cpp:47: parse error before `{' token
t1.cpp: In function `int main(int, char**)':
t1.cpp:62: `iFlt' undeclared (first use this function)
t1.cpp:62: (Each undeclared identifier is reported only once for each
function
it appears in.)
t1.cpp:63: `dFlt' undeclared (first use this function)

Dec 4 '05 #3
thanks a lot.

also i'd found some snippet on specialization without <> like:

template class vector<int>;

what is it?

p.s. i found your reply after deleting my original post

Dec 4 '05 #4
thanks a lot.

also i'd found some snippet on specialization without <> like:

template class vector<int>;

what is it?

p.s. i found your reply after deleting my original post

Neelesh Bodas wrote:
he****@gmail.co m wrote:
i want to explicit instantiate a member function of a class template,
and got some error,
can anyone give some advice to correct it?


Best advice is to understand the compiler-given errors and fix them one
by one.

complier: g++ 3.2

#include <iostream>
#include <string>
using namespace std;

template <class BT> class FilterRangeItem {
public:
bool m_bIsSetLow;
bool m_bIsSetHigh;
bool m_bIsFailed;
BT m_low;
BT m_high;


It is a *very_bad* idea to declare your data members all public.
FilterRangeItem ();
virtual BT _Convert(string );


Avoid using names that start with an underscore, they are reserved for
implementation.
virtual bool SetFilter(strin g,string);
};


If you expect the class to be polymorphic (as indicated by virtual
functions), always define a virtual destructor.


template <class BT>
FilterRangeItem <BT>::FilterRan geItem() {
m_bIsSetLow = m_bIsSetHigh = m_bIsFailed = false;
}


Use constructor-initialization list instead of assignments in the body
of the constructor.

[...snip...]
template int FilterRangeItem <int>::_Convert (string str) {
cout <<"FilterRangeI tem<int>::_Conv ert"<<endl;
return 0;


This is a complete specialization of the member template. Hence you
need to introduce empty angle-brackets after the word template.

template<> int FilterRangeItem <int>::_Convert (string str) {
}
template double FilterRangeItem <double>::_Conv ert(string str) {
cout <<"FilterRangeI tem<double>::_C onvert"<<endl;
return 0.0


Same here.
}

class Filter {
public:
FilterRangeItem <int> iFlt;
FilterRangeItem <double> dFlt;
};

int main(int argc, char* argv[])
{
{
Filter f;
iFlt.SetFilter( "3", "5");


Whats that? C++ needs variables to be declared before they are used.
dFlt.SetFilter( "6.0", "9.10");


Same.

Hope this helps.


Dec 4 '05 #5
thanks a lot.

also i'd found some snippet on specialization without <> like:

template class vector<int>;

what is it?

p.s. i found your reply after deleting my original post

Neelesh Bodas wrote:
he****@gmail.co m wrote:
i want to explicit instantiate a member function of a class template,
and got some error,
can anyone give some advice to correct it?
Best advice is to understand the compiler-given errors and fix them one
by one.

template <class BT>
FilterRangeItem <BT>::FilterRan geItem() {
m_bIsSetLow = m_bIsSetHigh = m_bIsFailed = false;
}


Use constructor-initialization list instead of assignments in the body
of the constructor.

[...snip...]
template int FilterRangeItem <int>::_Convert (string str) {
cout <<"FilterRangeI tem<int>::_Conv ert"<<endl;
return 0;


This is a complete specialization of the member template. Hence you
need to introduce empty angle-brackets after the word template.

template<> int FilterRangeItem <int>::_Convert (string str) {
}
template double FilterRangeItem <double>::_Conv ert(string str) {
cout <<"FilterRangeI tem<double>::_C onvert"<<endl;
return 0.0


Same here.
}


Same.

Hope this helps.


Dec 4 '05 #6

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

Similar topics

4
2601
by: C. Carbonera | last post by:
/* Hi, I have a problem with explicit instantiation of templates in Visual C++ 6.0. I have provided the source below. I have an example of a function template that produces incorrect output in the code below. The function template < typename T >
4
1836
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it elaborate to make sure my questions are clear enough. Let's say I need to write a function whose logic is same for all types (T) except in the case of T * (including const T *). Furtheremore , the function needs to be written differently for...
9
1504
by: Rennie deGraaf | last post by:
I'm writing a class similar to this: #include <iostream> using namespace std; template<class t> class Foo { private: class Bar {
3
4002
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something handed down from an ancestor or a predecessor or from the past: a legacy of religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF, from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
1
3070
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
1
8907
by: sparklight | last post by:
The following error arises when I try to compile tinybind, an apparently unsupported code project. (A compile-killing bug reported a year ago has gone unanswered.) So I'm looking for help outside. Apparently VC2003 introduced this error, C2768, and VC2005 made matters worse. Anyway, here's the template definition: template<class T> TiXmlBinding<T> const * GetTiXmlBinding( T const &, IdentityBase ); Here's a target class:
2
7688
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
4
2483
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
0
2912
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit instantiation of the partially specialized version, but my compiler (VC8) complains because it fails the SFINAE version. I just realized as I was typing this that I'm using partial _function_ specialization. I'm sure I remember reading somewhere that
0
8004
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
7934
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
8425
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
8418
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...
0
6743
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
5886
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...
0
5445
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.