compilation error calling static member function in template code

Discussion in 'C++' started by sunil, Aug 15, 2008.

  1. sunil

    sunil Guest

    Hello,
    I have a template class as follows which has a static function called
    foo, as you see below the class declaration, there are specializations
    for foo for different specializations of T, the problem I have is I
    cannot seem to get the syntax right for invoking foo() (since foo()
    is static member, I should be able to invoke the foo() on specialized
    class (no need for object)), if I uncomment first two lines in main()
    code it fails to compile, if its commented it complies and works as
    expected (note that bi.foo() and bf.foo() work properly).
    compiling this code on SUNOS, version of CC: CC: Sun C++ 5.5 Patch
    113817-12 2005/03/08
    any help greately appreciated.
    Thanks,
    Sunil
    #include "temp1.h"
    typedef b<int,COLOR_GREEN> BINT;
    typedef b<float,COLOR_RED> BFLOAT;
    void main()
    {

    //BINT.foo();
    //BFLOAT.foo();
    BINT bi(10);
    BFLOAT bf(12.2);
    bi.print();
    bf.print();
    bi.foo();
    bf.foo();
    }
    temp1.h:
    #include <iostream>
    using namespace std;

    typedef enum {
    COLOR_RED,
    COLOR_GREEN,
    COLOR_BLUE
    }ColorType;
    template <class T,ColorType color>
    class b
    {
    public:

    b(T t): _data(t) {
    }

    ~b() {
    }

    static void foo();

    void print() {
    cout << "Value=" << _data << endl;
    switch(color) {
    case COLOR_RED:
    cout << "specialized with RED color" << endl;
    break;
    case COLOR_GREEN:
    cout << "specialized with GREEN color" << endl;
    break;
    case COLOR_BLUE:
    cout << "specialized with BLUE color" << endl;
    break;
    }

    }


    private:
    T _data;

    } ;


    template <>
    void
    b<int,COLOR_GREEN>::foo() {
    cout << "foo called with instantiation int,COLOR_GREEN" << endl;
    }

    template <>
    void
    b<float,COLOR_RED>::foo() {
    cout << "foo called with instantiation float,COLOR_RED" << endl;
    }
    sunil, Aug 15, 2008
    #1
    1. Advertising

  2. sunil

    Ian Collins Guest

    Re: compilation error calling static member function in templatecode

    sunil wrote:
    > Hello,
    > I have a template class as follows which has a static function called
    > foo, as you see below the class declaration, there are specializations
    > for foo for different specializations of T, the problem I have is I
    > cannot seem to get the syntax right for invoking foo() (since foo()
    > is static member, I should be able to invoke the foo() on specialized
    > class (no need for object)),


    You don't specialise the class, you attempt to specialise foo.

    > void main()


    Don't ignore your compiler warnings.

    --
    Ian Collins.
    Ian Collins, Aug 15, 2008
    #2
    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. 0to60
    Replies:
    4
    Views:
    515
    jeffc
    Nov 21, 2003
  2. aling
    Replies:
    6
    Views:
    479
    Xiaobin.Huang
    Oct 30, 2005
  3. , India
    Replies:
    2
    Views:
    315
  4. dolphin
    Replies:
    3
    Views:
    1,342
    Pete Becker
    Dec 5, 2007
  5. Olumide
    Replies:
    9
    Views:
    510
Loading...

Share This Page