Problem with generic function

This is a discussion on Problem with generic function within the C++ Programming forums, part of the General Programming Boards category; Hello,
I am new to C++. I tried to create a function for doing the following:
1. Read a matrix ...

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

Then, I read somewhere, that it was possible that the compiler was not understanding the template thing, so I should move the definition of the function itself to the header. I did so, and after that i receive the complaint message originally posted

Code:

sscr.h|9|error C2065: 'T' : undeclared identifier

Which is, I think, the same as the first complaint before the moving.
Hope I have shed some light on the facts leading to the problem.
Thank you, Laserlight.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

Thank you!

Hello Laserlight,

The first alternative in the link I had already tried. It was the one that generated only one error message. The second one, including in the main file the .cpp file of the function, made things worse.
I think, I will spend some more time reading around about these functions, and will update when I find a suitable solution.
Thanks for your patience.

The problem with your header is that it only declares the function template, but does not define it. So if the compiler encounters an instantiation of your function template, it tries to specialise it for the correct template parameter 'T' .
BUT: what can it specialise? A declaration? Ok, it will. But then the linker will protest since there is no function defined.

ps: there is an alternative way to use function templates : don't ' #include ' the header file, but 'export' the template:

for (vector<T>::const_iterator jter = iter.begin(); jter != iter.end(); jter++) {
cout<< *
no need for an explicit specialisation of the function, the compiler will deduce the template arguments for the function template.

Josuttis is not only an expert in C++, but also in writing down his knowledge in a clear way. His book (together with Vandevoorde) C++ Templates is an absolute must for everyone who wants to know every small detail but also engineering techniques of templates.

EDIT: Why do you de-reference the iterator in the loop declaration?! This line

Still not working

Thank you, MarkZWEERS

First of all, I am very new in C++. This is my first attempt to code something and I still have many doubts, even in details as simple as the use of the operators. I appreciate your comment about the de-referenciator for the iterators in the inner loop. I also had a doubt about it.
Ok. Now the core problem with the generic function. I implemented the changes you suggested, but alas, still nothing. I am using codeblocks and MS visual C++ 2005/2008 compiler and I received this complaints:

Code:

||=== test01, Debug ===|
d:\dogma\data\research\codes\zcppzone\test01\sscr.h|19|error C2039: 'begin' : is not a member of 'std::_Vector_const_iterator<_Ty,_Alloc>'|
d:\dogma\data\research\codes\zcppzone\test01\sscr.h|19|error C2039: 'end' : is not a member of 'std::_Vector_const_iterator<_Ty,_Alloc>'|
d:\dogma\data\research\codes\zcppzone\test01\sscr.h|20|error C2039: 'end' : is not a member of 'std::_Vector_const_iterator<_Ty,_Alloc>'|
d:\dogma\data\research\codes\zcppzone\test01\sscr.h|21|error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::vector<_Ty>' (or there is no acceptable conversion)|
d:\dogma\data\research\codes\zcppzone\test01\sscr.h|23|error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::vector<_Ty>' (or there is no acceptable conversion)|
||=== Build finished: 5 errors, 0 warnings ===|

Update

Hey fellas,

I just did something interesting (at last!).
I thought the problem could be not in the definition on the layout of the generic function or the way that the compiler was interpreting the thing but in the special type I am using in the input vector<vector<int> >. So I modify the code to just send to screen a message instead of the elements of the matrix. I rewrote the body like this: