template

Hi
Is it's possible to create a class specific template function function in C++, some thing the code below. I wants to achieve something like below.
Please guide me.

1
2
3
4
5
6
7
8
9
10
class SomeClass {
public:
    template<typename T>
    void someFun(T in);
}

template<typename T>
void SomeClass::someFun(T in) {
    std::cout << in << endl;
}


Thanks!
Yes, that's almost right. You need to fix a bit the definition:
7
8
9
10
template<typename T>
void SomeClass::someFun<T>(T in) { // there
    std::cout << in << endl;
}


- And of course you'll need to keep everything in header files -
Last edited on
Thanks Bazzy for your suggestion but i'm getting error which I ve mentioned below. If you don't mind please guide me regarding this.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

class SomeClass {
public:
    template<typename T>
    void someFun(T in);
}

template<typename T>
void SomeClass::someFun<T>(T in) { // there
    std::cout << in << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}


and m getting error as

1
1>Deleting intermediate and output files for project 'hello', configuration 'Debug|Win32'
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>hello.cpp
1>c:\documents and settings\vivek.kumar\desktop\hello\hello.cpp(14) : error C2628: 'SomeClass' followed by 'void' is illegal (did you forget a ';'?)
1>c:\documents and settings\vivek.kumar\desktop\hello\hello.cpp(14) : error C2065: 'T' : undeclared identifier
1>c:\documents and settings\vivek.kumar\desktop\hello\hello.cpp(14) : error C2146: syntax error : missing ')' before identifier 'in'
1>c:\documents and settings\vivek.kumar\desktop\hello\hello.cpp(14) : error C2761: 'void SomeClass::someFun(T)' : member function redeclaration not allowed
1>c:\documents and settings\vivek.kumar\desktop\hello\hello.cpp(14) : error C2059: syntax error : ')'
1>c:\documents and settings\vivek.kumar\desktop\hello\hello.cpp(14) : error C2143: syntax error : missing ';' before '{'
1>c:\documents and settings\vivek.kumar\desktop\hello\hello.cpp(14) : error C2447: '{' : missing function header (old-style formal list?)
1>Build log was saved at "file://c:\Documents and Settings\vivek.kumar\Desktop\hello\Debug\BuildLog.htm"
1>hello - 7 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
You forgot the semicolon after the class body
Hi Bazzy thanks again,

Sorry for that silly error, But now I'm getting a new error as

error C2768: 'SomeClass::someFun' : illegal use of explicit template arguments
Oh, yes. Your first code was right, just with the semicolon
@Bazzy

Thanks alot for giving me your time and helping me out.
Topic archived. No new replies allowed.