?explicit instantiation member function of class template

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • henkoo@gmail.com

    ?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)

  • Neelesh Bodas

    #2
    Re: ?explicit instantiation member function of class template


    henkoo@gmail.co m wrote:[color=blue]
    > i want to explicit instantiate a member function of a class template,
    > and got some error,
    > can anyone give some advice to correct it?[/color]

    Best advice is to understand the compiler-given errors and fix them one
    by one.[color=blue]
    >
    > 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;[/color]

    It is a *very_bad* idea to declare your data members all public.
    [color=blue]
    > FilterRangeItem ();
    > virtual BT _Convert(string );[/color]

    Avoid using names that start with an underscore, they are reserved for
    implementation.
    [color=blue]
    > virtual bool SetFilter(strin g,string);
    > };[/color]

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

    [color=blue]
    >
    > template <class BT>
    > FilterRangeItem <BT>::FilterRan geItem() {
    > m_bIsSetLow = m_bIsSetHigh = m_bIsFailed = false;
    > }[/color]

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

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

    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) {
    [color=blue]
    > }
    > template double FilterRangeItem <double>::_Conv ert(string str) {
    > cout <<"FilterRangeI tem<double>::_C onvert"<<endl;
    > return 0.0[/color]

    Same here.
    [color=blue]
    > }
    >
    > class Filter {
    > public:
    > FilterRangeItem <int> iFlt;
    > FilterRangeItem <double> dFlt;
    > };
    >
    > int main(int argc, char* argv[])
    > {
    > {
    > Filter f;
    > iFlt.SetFilter( "3", "5");[/color]

    Whats that? C++ needs variables to be declared before they are used.
    [color=blue]
    > dFlt.SetFilter( "6.0", "9.10");[/color]

    Same.

    Hope this helps.

    Comment

    • Viktor Prehnal

      #3
      Re: ?explicit instantiation member function of class template

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

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


      henkoo@gmail.co m wrote:[color=blue]
      > 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)
      >[/color]

      Comment

      • henkoo@gmail.com

        #4
        Re: ?explicit instantiation member function of class template

        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

        Comment

        • henkoo@gmail.com

          #5
          Re: ?explicit instantiation member function of class template

          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:[color=blue]
          > henkoo@gmail.co m wrote:[color=green]
          > > i want to explicit instantiate a member function of a class template,
          > > and got some error,
          > > can anyone give some advice to correct it?[/color]
          >
          > Best advice is to understand the compiler-given errors and fix them one
          > by one.[color=green]
          > >
          > > 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;[/color]
          >
          > It is a *very_bad* idea to declare your data members all public.
          >[color=green]
          > > FilterRangeItem ();
          > > virtual BT _Convert(string );[/color]
          >
          > Avoid using names that start with an underscore, they are reserved for
          > implementation.
          >[color=green]
          > > virtual bool SetFilter(strin g,string);
          > > };[/color]
          >
          > If you expect the class to be polymorphic (as indicated by virtual
          > functions), always define a virtual destructor.
          >
          >[color=green]
          > >
          > > template <class BT>
          > > FilterRangeItem <BT>::FilterRan geItem() {
          > > m_bIsSetLow = m_bIsSetHigh = m_bIsFailed = false;
          > > }[/color]
          >
          > Use constructor-initialization list instead of assignments in the body
          > of the constructor.
          >
          > [...snip...]
          >[color=green]
          > > template int FilterRangeItem <int>::_Convert (string str) {
          > > cout <<"FilterRangeI tem<int>::_Conv ert"<<endl;
          > > return 0;[/color]
          >
          > 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) {
          >[color=green]
          > > }
          > > template double FilterRangeItem <double>::_Conv ert(string str) {
          > > cout <<"FilterRangeI tem<double>::_C onvert"<<endl;
          > > return 0.0[/color]
          >
          > Same here.
          >[color=green]
          > > }
          > >
          > > class Filter {
          > > public:
          > > FilterRangeItem <int> iFlt;
          > > FilterRangeItem <double> dFlt;
          > > };
          > >
          > > int main(int argc, char* argv[])
          > > {
          > > {
          > > Filter f;
          > > iFlt.SetFilter( "3", "5");[/color]
          >
          > Whats that? C++ needs variables to be declared before they are used.
          >[color=green]
          > > dFlt.SetFilter( "6.0", "9.10");[/color]
          >
          > Same.
          >
          > Hope this helps.[/color]

          Comment

          • henkoo@gmail.com

            #6
            Re: ?explicit instantiation member function of class template

            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:[color=blue]
            > henkoo@gmail.co m wrote:[color=green]
            > > i want to explicit instantiate a member function of a class template,
            > > and got some error,
            > > can anyone give some advice to correct it?[/color]
            >
            > Best advice is to understand the compiler-given errors and fix them one
            > by one.[/color]
            [color=blue][color=green]
            > > template <class BT>
            > > FilterRangeItem <BT>::FilterRan geItem() {
            > > m_bIsSetLow = m_bIsSetHigh = m_bIsFailed = false;
            > > }[/color]
            >
            > Use constructor-initialization list instead of assignments in the body
            > of the constructor.
            >
            > [...snip...]
            >[color=green]
            > > template int FilterRangeItem <int>::_Convert (string str) {
            > > cout <<"FilterRangeI tem<int>::_Conv ert"<<endl;
            > > return 0;[/color]
            >
            > 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) {
            >[color=green]
            > > }
            > > template double FilterRangeItem <double>::_Conv ert(string str) {
            > > cout <<"FilterRangeI tem<double>::_C onvert"<<endl;
            > > return 0.0[/color]
            >
            > Same here.
            >[color=green]
            > > }[/color][/color]
            [color=blue]
            >
            > Same.
            >
            > Hope this helps.[/color]

            Comment

            Working...