mardi 6 janvier 2015

problems in c++ programing - vector iterator

I am tring to compile this code and I have some errors. Can anyone help me with this??


The errors are :



1. Description Resource Path Location Type
conversion from 'std::vector<mtm::Element<double>, std::allocator<mtm::Element<double> > >::const_iterator {aka __gnu_cxx::__normal_iterator<const mtm::Element<double>*, std::vector<mtm::Element<double>, std::allocator<mtm::Element<double> > > >}' to non-scalar type 'mtm::SparseMatrix<double>::row_type::const_iterator' requested sm_example.cpp /hw4 line 30 C/C++ Problem
2. Description Resource Path Location Type
conversion from 'std::vector<mtm::Element<double>, std::allocator<mtm::Element<double> > >' to non-scalar type 'mtm::SparseMatrix<double>::row_const_iterator' requested sm_example.cpp /hw4 line 26 C/C++ Problem
3. Description Resource Path Location Type
no match for 'operator!=' (operand types are 'mtm::SparseMatrix<double>::row_const_iterator' and 'std::vector<mtm::Element<double>, std::allocator<mtm::Element<double> > >') sm_example.cpp /hw4 line 26 C/C++ Problem


The code is:



typedef mtm::SparseMatrix<double> dmat;
for(dmat::row_const_iterator row_it=m1.begin(); row_it!=m1.end(); ++row_it)
{
int row_index = row_it->index;
const dmat::row_type& row = row_it->get();
for (dmat::row_type::const_iterator col_it = row.begin(); col_it != row.end();++col_it)
{
int col_index = col_it->index;
double value = col_it->get();
std::cout << ++i << ": m1(" << row_index << "," << col_index << ")=" << value << std::endl;
}
}


This is the h file #ifndef SPARSE_H #define SPARSE_H_ #include #include using std::vector;



namespace mtm {

template<class T>
class Element {
T data;
public:
Element(int index, T data);
Element(const Element& element);
~Element();
int index;
int getIndex();
T get();
};

template<class T>
class SparseMatrix: public Element<T>, public vector<Element<T> > {
int numberOfRows, numberOfCulomns;
public:
SparseMatrix(int numberOfRows, int numberOfCulomns);
SparseMatrix(const SparseMatrix& matrix);
~SparseMatrix();
vector<Element<T> > begin();
vector<Element<T> > end();

const T operator()(const int rowIndex, const int colIndex) const;
T& operator()(const int rowIndex, const int colIndex);
SparseMatrix operator+(const SparseMatrix& matrix) const;
SparseMatrix& operator+=(const SparseMatrix& matrix);
SparseMatrix operator*(const SparseMatrix& matrix) const;
int get_row_count();
int get_col_count();
int get_non_zero_cells();

class row_type: public vector<Element<T> > {
vector<Element<T> > elements;
public:
row_type(int index);
row_type(const row_type& row);
~row_type();
int index;
row_type& get() const;

Element<T> getElements();
class const_iterator: public vector<Element<T> >::iterator {
Element<T>& elementConstIt;
public:
const_iterator();
~const_iterator();
int index;
Element<T> const& get();
};

class iterator {
Element<T>& elementIt;
public:
iterator();
~iterator();
int index;
Element<T>& get();
};
};

vector<row_type> rows;

class row_const_iterator: public vector<row_type>::const_iterator {
vector<row_type> rowConstIt;
public:
row_const_iterator(int index);
~row_const_iterator();
int index;
vector<row_type> get() const;
};

class row_iterator: public vector<row_type>::iterator {
vector<row_type>& rowIt;
public:
row_iterator();
~row_iterator();
int index;
vector<row_type>& get();
};

};
}

#endif /* SPARSE_H_ */


Thanks in advance for your answers.


Aucun commentaire:

Enregistrer un commentaire