Connecting Tech Pros Worldwide Forums | Help | Site Map

Templates Error

Newbie
 
Join Date: Jun 2007
Posts: 25
#1: Aug 8 '07
Hey guys,
Here is what I am doing:

Code:
Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. void func(vector<T> vec)
  3. {
  4.  ..
  5.  ..
  6.  ..
  7. }
  8.  
Now when I call this function as func<somestruct>(st); I get a compile time error? I am I calling the function wrongly?

gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#2: Aug 8 '07

re: Templates Error


Quote:

Originally Posted by creativeinspiration

Hey guys,
Here is what I am doing:

Code:

Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. void func(vector<T> vec)
  3. {
  4.  ..
  5.  ..
  6.  ..
  7. }
  8.  
Now when I call this function as func<somestruct>(st); I get a compile time error? I am I calling the function wrongly?

Hi,
I tried something similar and it worked well.
Expand|Select|Wrap|Line Numbers
  1. template<typename T>
  2. void func(vector<T> vec)
  3. {
  4. }
  5.  
  6. int main()
  7. {
  8.     vector<int> f1;
  9.     func(f1);
  10. }
  11.  
Raghuram
Newbie
 
Join Date: Jun 2007
Posts: 25
#3: Aug 8 '07

re: Templates Error


When I move the function to the same file it compiles fine. Is there a way I can keep this function in a .h file?
Member
 
Join Date: May 2007
Posts: 86
#4: Aug 8 '07

re: Templates Error


Quote:

Originally Posted by creativeinspiration

When I move the function to the same file it compiles fine. Is there a way I can keep this function in a .h file?

If you use the Comeau Compiler, you could use the export keyword, otherwise and unfortunately, most other compiler makers have chosen not to implement it and therefore your template definitions must be in a single file.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,370
#5: Aug 8 '07

re: Templates Error


Quote:

Originally Posted by Darryl

Comeau Compiler, you could use the export keyword,

Really? This is the first I've heard that any compiler supports export templates.

Quote:

Originally Posted by creativeinspiration

When I move the function to the same file it compiles fine. Is there a way I can keep this function in a .h file?

Put your templates in a header file and include the header as needed.
Reply