Hey guys,
Here is what I am doing:
Now when I call this function as func<somestruct>(st); I get a compile time error? I am I calling the function wrongly?Code:template <typename T>
void func(vector<T> vec)
{
..
..
..
}
Printable View
Hey guys,
Here is what I am doing:
Now when I call this function as func<somestruct>(st); I get a compile time error? I am I calling the function wrongly?Code:template <typename T>
void func(vector<T> vec)
{
..
..
..
}
What error? What is "st?" More info please.
st is the type of somestruct
If that's true, then it should give errors. Your function would expect a type of vector<somestruct>.Quote:
st is the type of somestruct
Post a minimal non-working sample if that doesn't fix it.
Before I had a function in a .h file. Once I move it to a cpp file it works fine. I have done it like this:
Is there a way I can keep the function in an external fileCode:template<typename T>
void func(vector<T>& vec)
{
}
int main()
{
vector<int> vec;
func(vec);
}
Templates must be included in all compilation units that use them. Putting it in a header file and then including the header should be no problem.
Simply moving it to a header file works. But if you put the function declaration in the .h file and definition in .cpp file that does not work.
That is to be expected. Why can't I separate the definition of my templates class from it's declaration and put it inside a .cpp file?Quote:
But if you put the function declaration in the .h file and definition in .cpp file that does not work.