Hello,
I am new to c++. I tried to create a function for doing the following:
1. Read a matrix from a .txt file
2. Store the retrieved data as a vector of vectors
3. Put the result in screen.
I could somehow complete parts 1 and 2. However, I am having lots of problems with the third part. I tried to use a generic function, and I am getting this complaint from my compiler:
d:\dogma\data\research\codes\zcppzone\test01\sscr. h|9|error C2065: 'T' : undeclared identifier
T there, is the type of the generic function. It is supposed to be ambiguous.
That is the problem. Any idea is appreciated. I am learnign this by myself so any kind of modification is welcome.
My codes:
Main function:
#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;
}
Generic function:
#include <iostream>
#include <vector>
#include "sscr.h"
using std::vector; using std::iostream;
template <class 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;
}
Generic function header:
#ifndef SSCR_H_INCLUDED
#define SSCR_H_INCLUDED
#include <iostream>
#include <vector>
using std::vector; using std::iostream;
int sscr(std::vector<T>);
#endif // SSCR_H_INCLUDED
Thanks in advance.