Ciao a tutti! Sto cercando di scrivere un codice che legga dei dati da un file e li manipoli attraverso una classe FileManager. Però ho qualche problema nel templizzare la classe Filemanager affinchè gestisca dati generici, in particolare i bugs sono:
||=== Build: Debug in 5.1 (compiler: GNU GCC Compiler) ===|
In function `main':|
undefined reference to `FileManager<DataModel>::FileManager()'|
undefined reference to `FileManager<DataModel>::read_file(std::string)'|
qui il programma applicato ad una classe Datamodel:
//Main.cpp
#include "filemanager.h"
#include <iostream>
#include "datamodel.h"
int main ( int argc , char * argv [])
{
FileManager<DataModel> fm;
fm.read_file("new_numbers.txt");
fm.write_file("phone_book.txt");
fm.sort_file();
fm.write_file("sorted_phone_book.txt");
fm.write_screen();
return 0;
}
//FILEMANAGER.HPP
#ifndef FILE_MANAGER_H
#define FILE_MANAGER_H
# include <fstream>
# include <vector>
# include <string>
# include "datamodel.h"
using namespace std;
template<class T>
class FileManager
{
public :
FileManager ();
void read_file(string file_name);
void write_file(string file_name);
void write_screen();
void sort_file();
//DataModel & get_data();
private :
vector<T>_data ;
};
#endif // FILE_MANAGER_H
bool cmp(const DataModel& d1 , const DataModel& d2);
//FILEMANAGER.CPP
#include "filemanager.h"
#include <iostream>
#include <algorithm>
using namespace std ;
template<class T> FileManager<T>::FileManager ()
{
}
template<class T> void FileManager<T>::read_file(std::string file_name)
{
ifstream file;
file.open(file_name.c_str()); ///file.open vuole una char* e non una stringa. Allora converto la "stringa" nel senso C++
///in una stringa nel senso classico del C (array di char)
while (!file.eof())
{
std::string line;
getline(file, line); // read a line from the file
T new_data(line); // create a DataModel passing the line
_data.push_back(new_data); // push the data in the data vector
}
file.close();
}
template<class T> void FileManager<T>::write_file(std::string file_name)
{
ofstream file;
file.open(file_name.c_str());
for(unsigned int i=0; i<_data.size();++i)
{
_data.print(file);
}
file.close();
}
template<class T> void FileManager<T>::write_screen()
{
for (unsigned int i=0; i<_data.size();++i)
{
_data.print(std::cout);
}
}
/*bool cmp(const DataModel& d1, const DataModel& d2)
{
return (d1.surname() < d2.surname());
}
template<class T> void FileManager<T>::sort_file()
{
//sort(_data.begin(), _data.end() ,cmp );
///sono equivalenti le due scritture.. la sort vuole PUNTATORI agli estremi degli elementi da ordinare.
sort(&_data[0], &_data[_data.size()] ,cmp );
}
*/
/*template<class T> DataModel& FileManager<T>::get_data()
{
return _data;
}
*/
//DATAMODEL.H
#ifndef DATA_MODEL_H
#define DATA_MODEL_H
#include <string>
#include <ostream>
using namespace std;
class DataModel
{
public:
DataModel(string name, string surname, string number);
DataModel(const DataModel & to_copy);
DataModel(string line_entry);
void print(ostream &output_stream);
string surname() const;
string name() const;
private:
string _name, _surname, _phone_num;
};
#endif // DATA_MODEL_H
//DATAMODEL.CPP
#include "datamodel.h"
# include <sstream>
# include <fstream>
using namespace std ;
DataModel::DataModel (const DataModel &to_copy):
_name(to_copy._name),
_surname(to_copy._surname),
_phone_num(to_copy._phone_num)
{
}
DataModel::DataModel (std::string line_entry)
{
stringstream ss(line_entry);
ss >>_name>>_surname>>_phone_num;
}
DataModel :: DataModel ( std :: string name , std :: string surname , std :: string number ):
_name ( name ),
_surname ( surname ),
_phone_num ( number )
{
}
std::string DataModel::name() const
{
return _name;
}
std::string DataModel::surname() const
{
return _surname;
}
void DataModel :: print ( ostream & output_stream )
{
output_stream <<name()<<" "<< surname() << " " << _phone_num << std :: endl ;
}
___________________________________________________________________________________
Grazie a chi vorrà aiutarmi!!