473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

compilation error calling static member function in template code

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_GREENBINT;
typedef b<float,COLOR_REDBFLOAT;
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;
}
Aug 14 '08 #1
1 1471
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.
Aug 15 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: justin.adam.miller | last post by:
I've been trying to use the sfinae principle in some code and have been getting many compiler errors. So I decided to try a very simplified version to see if I had the idea correct. Here's the...
7
by: Ook | last post by:
What am I doing wrong? This code gives a compile error: 'SortedList<T>::insert' : illegal call of non-static member function. I've tried several variations of this, but keep getting the same error....
5
by: cranium.2003 | last post by:
hi, Here is my code #include <iostream.h> int main() { cout <<"HI"; return 0; } and using following command to compile a C++ program g++ ex1.cpp -o ex1
9
by: subramanian | last post by:
Hello. Consider the following code fragment : enum TestEnum { val1 = 10, val2 = 100, val3 = 1000 }; class Test { public : enum TestEnum { val1 = 1, val2 val3 }; Test(int i = 0, int j = 0,...
3
by: sam_cit | last post by:
Hi All, I have the following scenario, A class is declared in the header file and has a static member function sample() in sample.h sample.cc uses the static member function sample(). the...
6
by: pleexed | last post by:
hello, this is my first post in a newsgroup, i hope i do everything right :) first of all, i am sure there have been a lot of "are templates slow?" questions around, but i think what i would...
2
by: subramanian100in | last post by:
consider the following program #include <iostream> using namespace std; class Rec { public: Rec(int arg = 10) : val(arg) { }
1
by: BSand0764 | last post by:
I'm getting an error that I can't seem to resolve. When I compile the Functor related logic in a test program, the files compile and execute properly (see Listing #1). However, when I...
1
by: Alex Vinokur | last post by:
Hi, I have compilation problem on SUN CC compiler with template while using option -m64 (64-bit addressing model). No problem while using option -m32 (32-bit addressing model) $ CC -V CC:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.