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 ===|
Now, the updated code is this:
Code:
// Attempt to READ a matrix from a file: Successful
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "sscr.h"
using namespace std;
int main(){
// Variable declaration and initialization
string x;
typedef string::size_type string_size;
string_size i = 0, j, sz;
vector <vector<int> > adjm;
vector<int> data;
ifstream adj("adj.txt");
istringstream sxp;
int hi;
// Actions
getline(adj, x);
sxp.str(x);
sz = x.size() - 1;
while (i != sz){
for (j = 0; j != sz; ++j){
sxp >> hi;
data.push_back(hi);
}
adjm.push_back(data);
sxp.clear();
data.clear();
getline(adj, x);
sxp.str(x);
++i;
}
// Closing commands
cout << "the number of items in a row of the file is: " << sz << endl;
int p = sscr(adjm);
return p;
}
The only change here was to the line now depicting #include "sscr.h". The header sscr.h is this:
Code:
#ifndef SSCR_H_INCLUDED
#define SSCR_H_INCLUDED
#include <iostream>
#include <vector>
using std::vector; using std::iostream;
template<typename T>
int sscr(vector<T> v);
using std::vector; using std::iostream;
template <typename T>
int sscr(vector<T> v){
for (vector<T>::const_iterator iter = v.begin(); iter != v.end(); iter++){
for (vector<T>::const_iterator jter = iter.begin(); jter != iter.end(); jter++){
if (jter != iter.end() - 1)
cout << *jter << " ";
else
cout << *jter << endl;
}
}
return 0;
}
#endif // SSCR_H_INCLUDED
The changes here were the one that you pointed out in your comment.
Thanks in advance for your help.