By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,438 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Templates Error

P: 25
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?
Aug 8 '07 #1
Share this Question
Share on Google+
4 Replies


gpraghuram
Expert 100+
P: 1,275
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
Aug 8 '07 #2

P: 25
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?
Aug 8 '07 #3

P: 86
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.
Aug 8 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
Comeau Compiler, you could use the export keyword,
Really? This is the first I've heard that any compiler supports export templates.

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.
Aug 8 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.