473,473 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Create 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(string,string);
};

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

template <class BT>
BT FilterRangeItem<BT>::_Convert(string str) {
BT a;
cout <<"FilterRAngeItem<BT>::_Convert()"<<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 <<"FilterRangeItem<int>::_Convert"<<endl;
return 0;
}
template double FilterRangeItem<double>::_Convert(string str) {
cout <<"FilterRangeItem<double>::_Convert"<<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 2634

he****@gmail.com 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(string,string);
};
If you expect the class to be polymorphic (as indicated by virtual
functions), always define a virtual destructor.


template <class BT>
FilterRangeItem<BT>::FilterRangeItem() {
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 <<"FilterRangeItem<int>::_Convert"<<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>::_Convert(string str) {
cout <<"FilterRangeItem<double>::_Convert"<<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.com 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(string,string);
};

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

template <class BT>
BT FilterRangeItem<BT>::_Convert(string str) {
BT a;
cout <<"FilterRAngeItem<BT>::_Convert()"<<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 <<"FilterRangeItem<int>::_Convert"<<endl;
return 0;
}
template double FilterRangeItem<double>::_Convert(string str) {
cout <<"FilterRangeItem<double>::_Convert"<<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.com 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(string,string);
};


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


template <class BT>
FilterRangeItem<BT>::FilterRangeItem() {
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 <<"FilterRangeItem<int>::_Convert"<<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>::_Convert(string str) {
cout <<"FilterRangeItem<double>::_Convert"<<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.com 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>::FilterRangeItem() {
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 <<"FilterRangeItem<int>::_Convert"<<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>::_Convert(string str) {
cout <<"FilterRangeItem<double>::_Convert"<<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
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...
4
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...
9
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
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...
1
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
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....
2
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
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
0
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...
0
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,...
0
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...
0
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
1
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...
0
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...
1
muto222
php
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.