compilation error calling static member function in template code

S

sunil

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;
}
 
I

Ian Collins

sunil said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top