?explicit instantiation member function of class template

Discussion in 'C++' started by henkoo@gmail.com, Dec 4, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. 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.
    Neelesh Bodas, Dec 4, 2005
    #2
    1. Advertising

  3. specialization is done as, in VS.NET otherwise works....

    template<> int f(......)


    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)
    >
    Viktor Prehnal, Dec 4, 2005
    #3
  4. Guest

    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, 2005
    #4
  5. Guest

    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:
    > 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, 2005
    #5
  6. Guest

    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:
    > 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, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fernando Cuenca
    Replies:
    4
    Views:
    2,513
    Gianni Mariani
    Sep 6, 2004
  2. Thomas Maier-Komor
    Replies:
    6
    Views:
    601
    Thomas Maier-Komor
    May 19, 2005
  3. Replies:
    1
    Views:
    559
    Salt_Peter
    Dec 25, 2006
  4. highegg
    Replies:
    1
    Views:
    328
    Neelesh
    Jun 3, 2009
  5. Noah Roberts
    Replies:
    6
    Views:
    1,130
    Johannes Schaub (litb)
    Feb 2, 2011
Loading...

Share This Page