Template problem ...

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Programming Template problem ...
# 1  
Template problem ...

Hi all,
Need your help. I am doing a simple template program , getting some error ... here is the code

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
using namespace std;
class Base_class
Base_class(){ };
~Base_class(){ };
template <class T>
static int fun(T decimal_value,string& check)
ostringstream ostr;
string str=ostr.str();
return 0;
int main()
string check;
int a=7777;
Base_class ::fun(a,check);
double b=7777.9755;
char c[]="AMARTYA";
cout<<"Function Cutil::failed"<<endl;
return 0;

This code works fine ....
But i want the Definition of the function not in the main class , i want to put it outside function ......
If i do that ..... It is giving error .....
Please provide some solution(s)...
# 2  
You didn't post the code that doesn't work, but I guess that you want to define the method template in a separate file rather than in the definition of the class, as one often does for normal class methods.
Short answer; you can't.

There is, no doubt, a better explanation in a text book somewhere, but here goes...
Templates are not like normal code; you cannot define them where ever you like. When the compiler sees your call to
int a;

here is no method with signature
fun(int, string&);

so it has to create one from the template you supplied. This means that the compiler has to be able to see the both the template (so it knows how to build the function) and the call (so it knows what types to build the function with) at the same time. So the template and the code calling the method must be in the same compilation unit.
You don't have to have the method template defined inline, but you must have it defined where the class is defined.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #852
Difficulty: Medium
AngularJS is a JavaScript-based open-source front-end web framework mainly maintained by Facebook.
True or False?

6 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Cacti SNMP Generic OID template, NaN problem

Hello All, I have installed cacti and I have Nan values on created graph. I really appreciate any help. I have done the following: 1. Linux Ubuntu 12.04.2 LTS 64 bits 2. apt-get install cacti // the cacti Version 0.8.7i was installed 3. Accessed "http://MyIP/cacti/index.php" 4. Set SNMP... (0 Replies)
Discussion started by: AndreiM
0 Replies

2. Programming

Calling template at once

Hello Again, I am just wanted to know if we can call the Template using "require_once" at PHP? Any views around happy to discuss. Thanks in Advance (2 Replies)
Discussion started by: AimyThomas
2 Replies

3. Programming

C++ template error

I get some compiling errors about template instantiation :wall: , but I can't find where the syntax errors happens. Can some help me? template<typename Type> class SingleList; template<typename Type> class SingleListNode{ private: friend class SingleList<Type>; SingleListNode() :... (1 Reply)
Discussion started by: 915086731
1 Replies

4. Shell Programming and Scripting

Help with template like solution

hi experts, i'm trying to do this: file1 is a template. might have kinds of 'funny' characters. sample: <body> <form> <p><input type="text" name="abc"/></p> &nbsp; <p><my_content></p> </form> </body> file2 is a file that contains lots of text. this might be very big. might have... (2 Replies)
Discussion started by: xjohnu
2 Replies

5. UNIX for Dummies Questions & Answers

vi calling template

Hello. I want to copy temp files when I make a new file by vi. For example, 09:32:52 ~/ $ mkdir test 09:33:03 ~/ $ cd test/ 09:33:09 ~/test/ $ ls 09:33:16 ~/test/ $ vi test.cpp 09:34:37 ~/test/ $ cat test.cpp #include <iostream> int main() { } 09:34:48 ~/test/ $ vi test.bash 09:35:19... (1 Reply)
Discussion started by: Euler04
1 Replies

6. Programming

About template constraints

Hi, i have class template, 1)can i override the copy constructor 2)can we have virtual function in class template if not plz tel why? I tried , compile error comes for me... Thanks Sarwan (0 Replies)
Discussion started by: sarwan
0 Replies

Featured Tech Videos