| | |
Move template functions to a .cpp file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 628
Reputation:
Solved Threads: 46
According to this:
http://www.parashift.com/c++-faq-lit...html#faq-35.13
One way to keep only the function declaration in the .h file is to do this
However, what if you want this function for a type that Tools does not know about? ie
will not work because Point has not been defined. If you make Tools depend on Point, but Point already depends on Tools, then there is a circular problem.
Any suggestions?
Thanks,
Dave
http://www.parashift.com/c++-faq-lit...html#faq-35.13
One way to keep only the function declaration in the .h file is to do this
C++ Syntax (Toggle Plain Text)
////////// file: Tools.h #include <iostream> #include <vector> using namespace std; template <typename T> T Sum(vector<T> &V);
C++ Syntax (Toggle Plain Text)
///////// file: Tools.cpp #include "Tools.h" #include <iostream> #include <vector> using namespace std; template <typename T> T Sum(vector<T> &V) { T sum = static_cast<T>(0.0); for(unsigned int i = 0; i < V.size(); i++) sum += V[i]; return sum; } //this is the key line to tell the compiler to actually make this function for unsigned int template unsigned int Sum<unsigned int>(vector<unsigned int> &V);
However, what if you want this function for a type that Tools does not know about? ie
C++ Syntax (Toggle Plain Text)
template Point Sum<Point>(vector<Point> &V);
Any suggestions?
Thanks,
Dave
•
•
Join Date: Feb 2008
Posts: 628
Reputation:
Solved Threads: 46
C++ Syntax (Toggle Plain Text)
////// file: Point.h #include "Tools.h" class Point { public: double x,y,z; void OutputSum() { // do something using a function from Tools here } };
Then if I include Point in Tools is that not a problem?
•
•
Join Date: Feb 2008
Posts: 628
Reputation:
Solved Threads: 46
yup, its txx.
see section C.5 in this document:
http://public.kitware.com/vxl/doc/de...15.html#SEC158
see section C.5 in this document:
http://public.kitware.com/vxl/doc/de...15.html#SEC158
•
•
Join Date: Oct 2008
Posts: 40
Reputation:
Solved Threads: 6
•
•
•
•
yup, its txx.
see section C.5 in this document:
http://public.kitware.com/vxl/doc/de...15.html#SEC158
Thanx...
Sean
![]() |
Similar Threads
- error: expected constructor, destructor, or type conversion before '<' to (C++)
- C++ operator overloading.....problem with compiling (C++)
Other Threads in the C++ Forum
- Previous Thread: Re: How to use double pointers in C++?
- Next Thread: need help with matrix
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






