Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > compilation error calling static member function in template code

Reply
Thread Tools

compilation error calling static member function in template code

 
 
sunil
Guest
Posts: n/a
 
      08-14-2008
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;
}


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      08-15-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a static class-member template function Olumide C++ 9 02-05-2008 12:32 AM
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
class template member function - compilation error subramanian100in@yahoo.com, India C++ 2 11-02-2007 12:33 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
performance of static member function vs. instance member function 0to60 C++ 4 11-21-2003 05:25 PM



Advertisments