Bottom
asked on
C++:Error - redeclared as different kind of symbol
Could anybody kindly point out whatz wrong with the following as i have receive an error as:
Redeclared as different kind of symbol when using g++ to compile.
#ifndef associativematrix_h
#define associativematrix_h
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
template <typename T> class AssociativeMatrixData;
template <typename T>
class AssociativeMatrixIterator
{
public:
typedef T* pointer;
typedef T& reference;
AssociativeMatrixIterator& operator ++() { ++*this; return *this; }
AssociativeMatrixIterator& operator --() { --*this; return *this; }
AssociativeMatrixIterator operator ++(int)
{
AssociativeMatrixIterator temp(*this);
++*this;
return temp;
}
AssociativeMatrixIterator operator --(int)
{
AssociativeMatrixIterator temp(*this);
--*this;
return temp;
}
AssociativeMatrixIterator operator =(const AssociativeMatrixIterator& rhs )
{
this = rhs;
return this;
}
reference operator*()
{
return *operator->();
}
pointer operator ->()
{
return &this.begin();
}
bool operator ==(AssociativeMatrixIterat or& s)
{
return this == s;
}
bool operator !=(AssociativeMatrixIterat or& rhs)
{
return !( this == rhs );
}
};
template <typename T>
class Row
{
public:
Row();
Row(const Row& s); // copy constructor
T& operator[]( const string& s );
T operator[]( const string& s ) const;
Row<T>& operator=( const Row<T>& s );
private:
vector<string> R_vStr;
vector<T> R_vData;
};
template <typename T>
class AssociativeMatrix
{
public:
AssociativeMatrixData<T>* Data;
AssociativeMatrix();
AssociativeMatrix( const AssociativeMatrix& s); // copy constructor
//~AssociativeMatrix();
Row<T>& operator[]( const string& s );
Row<T> operator[]( const string& s ) const;
AssociativeMatrix<T>& operator =( const AssociativeMatrix<T>& s );
size_t getRefCount() const;
typedef AssociativeMatrixIterator< T> iterator;
iterator begin() { return vData.begin(); }
iterator end() { return vData.end(); }
iterator& operator++();
private:
};
template <typename T>
class AssociativeMatrixData
{
public:
friend class AssociativeMatrix;
int refCount;
vector<string> vStr;
vector<Row<T> > vData;
private:
void increment();
void decrement();
};
#include "AssociativeMatrix.tem"
#endif
//AssociativeMatrix.tem
#include "associativematrix.h"
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template<typename T>
Row<T>::Row() { }
template<typename T>
Row<T>::Row( const Row& s )
{
R_vStr = s.R_vStr;
R_vData = s.R_vData;
}
template<typename T>
T& Row<T>::operator[]( const string& str )
{
vector<string>::iterator it;
for (it = R_vStr.begin(); it != R_vStr.end(); it++)
{
if (*it == str)
return R_vData[it - R_vStr.begin()];
}
R_vStr.push_back(str);
R_vData.push_back(0);
//cout << "Row Size : " << R_vData.size() << endl;
return *(R_vData.end() - 1);
}
template<typename T>
T Row<T>::operator[]( const string& str ) const
{
vector<string>::iterator it;
for (it = R_vStr.begin(); it != R_vStr.end(); it++)
{
if (*it == str)
return R_vData[it - R_vStr.begin()];
}
R_vStr.push_back(str);
R_vData.push_back(0);
return *(R_vData.end() - 1);
}
template <typename T>
Row<T>& Row<T>::operator=( const Row<T>& s )
{
R_vStr = s.R_vStr;
R_vData = s.R_vData;
return *this;
}
template<typename T>
AssociativeMatrix<T>::Asso ciativeMat rix()
{
Data = new AssociativeMatrixData<T>() ;
Data -> refCount = 1;
}
template<typename T>
AssociativeMatrix<T>::Asso ciativeMat rix( const AssociativeMatrix& s )
{
Data = s->Data;
Data->increment();
}
template<typename T>
Row<T>& AssociativeMatrix<T>::oper ator[]( const string& str )
{
vector<string>::iterator it;
for (it = Data->vStr.begin(); it != Data->vStr.end(); it++)
{
if (*it == str)
return (Data->vData[it - Data->vStr.begin()]);
}
Row<T> row;
Data->vStr.push_back(str);
Data->vData.push_back(row) ;
return *(Data->vData.end()-1);
}
template<typename T>
Row<T> AssociativeMatrix<T>::oper ator[]( const string& str ) const
{
vector<string>::iterator it;
for (it = Data->vStr.begin(); it != Data->vStr.end(); it++)
{
if (*it == str)
return (Data->vData[it - Data->vStr.begin()]);
}
incrementCount(); // increase refCount;
Row<T> row;
Data->vStr.push_back(str);
Data->vData.push_back(row) ;
return *(Data->vData.end()-1);
}
template <typename T>
AssociativeMatrix<T>& AssociativeMatrix<T>::oper ator =( const AssociativeMatrix<T>& s )
{
Data->decrement();
Data = s->Data;
Data->increment();
return *this;
}
template <typename T>
size_t AssociativeMatrix<T>::getR efCount() const
{
return Data->refCount;
}
template <typename T>
void AssociativeMatrixData<T>:: increment( )
{
refCount++;
}
template <typename T>
void AssociativeMatrixData<T>:: decrement( )
{
refCount--;
if(refCount == 0) delete this;
}
Redeclared as different kind of symbol when using g++ to compile.
#ifndef associativematrix_h
#define associativematrix_h
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
template <typename T> class AssociativeMatrixData;
template <typename T>
class AssociativeMatrixIterator
{
public:
typedef T* pointer;
typedef T& reference;
AssociativeMatrixIterator&
AssociativeMatrixIterator&
AssociativeMatrixIterator operator ++(int)
{
AssociativeMatrixIterator temp(*this);
++*this;
return temp;
}
AssociativeMatrixIterator operator --(int)
{
AssociativeMatrixIterator temp(*this);
--*this;
return temp;
}
AssociativeMatrixIterator operator =(const AssociativeMatrixIterator&
{
this = rhs;
return this;
}
reference operator*()
{
return *operator->();
}
pointer operator ->()
{
return &this.begin();
}
bool operator ==(AssociativeMatrixIterat
{
return this == s;
}
bool operator !=(AssociativeMatrixIterat
{
return !( this == rhs );
}
};
template <typename T>
class Row
{
public:
Row();
Row(const Row& s); // copy constructor
T& operator[]( const string& s );
T operator[]( const string& s ) const;
Row<T>& operator=( const Row<T>& s );
private:
vector<string> R_vStr;
vector<T> R_vData;
};
template <typename T>
class AssociativeMatrix
{
public:
AssociativeMatrixData<T>* Data;
AssociativeMatrix();
AssociativeMatrix( const AssociativeMatrix& s); // copy constructor
//~AssociativeMatrix();
Row<T>& operator[]( const string& s );
Row<T> operator[]( const string& s ) const;
AssociativeMatrix<T>& operator =( const AssociativeMatrix<T>& s );
size_t getRefCount() const;
typedef AssociativeMatrixIterator<
iterator begin() { return vData.begin(); }
iterator end() { return vData.end(); }
iterator& operator++();
private:
};
template <typename T>
class AssociativeMatrixData
{
public:
friend class AssociativeMatrix;
int refCount;
vector<string> vStr;
vector<Row<T> > vData;
private:
void increment();
void decrement();
};
#include "AssociativeMatrix.tem"
#endif
//AssociativeMatrix.tem
#include "associativematrix.h"
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template<typename T>
Row<T>::Row() { }
template<typename T>
Row<T>::Row( const Row& s )
{
R_vStr = s.R_vStr;
R_vData = s.R_vData;
}
template<typename T>
T& Row<T>::operator[]( const string& str )
{
vector<string>::iterator it;
for (it = R_vStr.begin(); it != R_vStr.end(); it++)
{
if (*it == str)
return R_vData[it - R_vStr.begin()];
}
R_vStr.push_back(str);
R_vData.push_back(0);
//cout << "Row Size : " << R_vData.size() << endl;
return *(R_vData.end() - 1);
}
template<typename T>
T Row<T>::operator[]( const string& str ) const
{
vector<string>::iterator it;
for (it = R_vStr.begin(); it != R_vStr.end(); it++)
{
if (*it == str)
return R_vData[it - R_vStr.begin()];
}
R_vStr.push_back(str);
R_vData.push_back(0);
return *(R_vData.end() - 1);
}
template <typename T>
Row<T>& Row<T>::operator=( const Row<T>& s )
{
R_vStr = s.R_vStr;
R_vData = s.R_vData;
return *this;
}
template<typename T>
AssociativeMatrix<T>::Asso
{
Data = new AssociativeMatrixData<T>()
Data -> refCount = 1;
}
template<typename T>
AssociativeMatrix<T>::Asso
{
Data = s->Data;
Data->increment();
}
template<typename T>
Row<T>& AssociativeMatrix<T>::oper
{
vector<string>::iterator it;
for (it = Data->vStr.begin(); it != Data->vStr.end(); it++)
{
if (*it == str)
return (Data->vData[it - Data->vStr.begin()]);
}
Row<T> row;
Data->vStr.push_back(str);
Data->vData.push_back(row)
return *(Data->vData.end()-1);
}
template<typename T>
Row<T> AssociativeMatrix<T>::oper
{
vector<string>::iterator it;
for (it = Data->vStr.begin(); it != Data->vStr.end(); it++)
{
if (*it == str)
return (Data->vData[it - Data->vStr.begin()]);
}
incrementCount(); // increase refCount;
Row<T> row;
Data->vStr.push_back(str);
Data->vData.push_back(row)
return *(Data->vData.end()-1);
}
template <typename T>
AssociativeMatrix<T>& AssociativeMatrix<T>::oper
{
Data->decrement();
Data = s->Data;
Data->increment();
return *this;
}
template <typename T>
size_t AssociativeMatrix<T>::getR
{
return Data->refCount;
}
template <typename T>
void AssociativeMatrixData<T>::
{
refCount++;
}
template <typename T>
void AssociativeMatrixData<T>::
{
refCount--;
if(refCount == 0) delete this;
}
builds OK in VS7/VC++
ASKER
ops .. guess i forgot to put up the test.cpp file.
#include "associativematrix.h"
int main()
{
AssociativeMatrix<float> M;
AssociativeMatrix<float> O;
M["monday"]["am"] = 9.8;
M["wednesday"]["am"] = 8.7;
M["thursday"]["pm"] = 3.2;
M["monday"]["pm"] = 8.1;
M["monday"]["am"] = 3.1;
M["saturday"]["pm"] = 7.8;
M["sunday"]["pm"] = 1.1;
M["friday"]["am"] = 8.3;
cout << M["monday"]["am"] << endl;
cout << M["monday"]["pm"] << endl;
cout << M["wednesday"]["am"] << endl;
cout << M["wednesday"]["pm"] << endl;
cout << M["thursday"]["am"] << endl;
cout << M["thursday"]["pm"] << endl;
cout << M["saturday"]["am"] << endl;
cout << M["saturday"]["pm"] << endl;
cout << M["sunday"]["am"] << endl;
cout << M["sunday"]["pm"] << endl;
cout << M["friday"]["am"] << endl;
cout << M["friday"]["pm"] << endl << endl;
O = M;
AssociativeMatrix<float>:: iterator iter;
for(iter = M.begin(); iter != M.end(); ++iter)
cout <<*iter << "";
cout << M.getRefCount() << endl;
cout << O.getRefCount() << endl;
return 0;
}
#include "associativematrix.h"
int main()
{
AssociativeMatrix<float> M;
AssociativeMatrix<float> O;
M["monday"]["am"] = 9.8;
M["wednesday"]["am"] = 8.7;
M["thursday"]["pm"] = 3.2;
M["monday"]["pm"] = 8.1;
M["monday"]["am"] = 3.1;
M["saturday"]["pm"] = 7.8;
M["sunday"]["pm"] = 1.1;
M["friday"]["am"] = 8.3;
cout << M["monday"]["am"] << endl;
cout << M["monday"]["pm"] << endl;
cout << M["wednesday"]["am"] << endl;
cout << M["wednesday"]["pm"] << endl;
cout << M["thursday"]["am"] << endl;
cout << M["thursday"]["pm"] << endl;
cout << M["saturday"]["am"] << endl;
cout << M["saturday"]["pm"] << endl;
cout << M["sunday"]["am"] << endl;
cout << M["sunday"]["pm"] << endl;
cout << M["friday"]["am"] << endl;
cout << M["friday"]["pm"] << endl << endl;
O = M;
AssociativeMatrix<float>::
for(iter = M.begin(); iter != M.end(); ++iter)
cout <<*iter << "";
cout << M.getRefCount() << endl;
cout << O.getRefCount() << endl;
return 0;
}
I'm looking
Try making an instance of
AssociativeMatrixIterator< float> iZ;
that's the first problem
Try making an instance of
AssociativeMatrixIterator<
that's the first problem
ASKER
I guess the iterator part is having a BIG problem ... maybe the begin() is returning a wrong value too. Isnt it the begin() should return the 1st position of the container? The 1st posibition of the container is referring to Row<T> or vData since the Row<T> is the container ... the error is like converting from float * to AssociativeMatrixIterator ... is required
ASKER
anybody having any ides how can i access to every single elements of the Matrix from operator []?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Tks Akunite ... I am aware the code is full of errors, but ...
Anyway ... now having problem with iterator, wonder if u could help?
The compiler always give me the msg that there isnt iterator template in the std. The code is as follow:
//associativematrix.h
#ifndef associativematrix_h
#define associativematrix_h
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
template <typename T> class AssociativeMatrixIterator;
template <typename T>
class Row
{
public:
friend class AssociativeMatrix<T>;
Row();
Row(const Row& s); // copy constructor
T& operator[]( const string& s );
T operator[]( const string& s ) const;
Row<T>& operator=( const Row<T>& s );
typedef AssociativeMatrixIterator< T> iterator;
iterator begin() { return iterator(R_vData,R_vData.b egin()); }
iterator end() { return iterator(R_vData,R_vData.b egin()); }
private:
vector<string> R_vStr;
vector<T> R_vData;
};
template <typename T>
class AssociativeMatrixIterator: public std::iterator<std::bidirec tional_ite rator_tag, T>
{
public:
typedef T* pointer;
typedef T& reference;
friend class Row<T>;
friend class AssociativeMatrix<T>;
AssociativeMatrixIterator( vector<T>& lst, const typename vector<T>::iterator& i)
: r(&lst), it(i) {}
AssociativeMatrixIterator& operator ++();
AssociativeMatrixIterator& operator --();
AssociativeMatrixIterator operator ++(int);
AssociativeMatrixIterator operator --(int);
reference operator*() const
{
return *it;
}
//template <typename T>
//T* AssociativeIterator<T>::op erator->() { return &(*_currentCell); }
pointer operator ->()
{
return &(it);
}
bool operator ==(const AssociativeMatrixIterator& s)
{
return it == s.it;
}
bool operator !=(const AssociativeMatrixIterator& s)
{
return !(*this == s );
}
protected:
vector<T>::iterator it;
vector<T>* r;
};
#include "AssociativeMatrix.tem"
#endif
#include "associativematrix.h"
#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<string> test;
template<typename T>
Row<T>::Row() { }
template<typename T>
Row<T>::Row( const Row<T>& s )
{
R_vStr = s.R_vStr;
R_vData = s.R_vData;
}
template<typename T>
T& Row<T>::operator[]( const string& str )
{
int update = -1;
int zero = test.size() - R_vData.size();
/*
cout << "test.size() a : " << test.size() << endl;
cout << "R_vStr.size() a : " << R_vStr.size() << endl;
cout << "R_vData.size() a : " << R_vData.size() << endl;
cout << "zero a : " << zero << endl;
*/
vector<string>::iterator s;
vector<string>::iterator it;
if(zero > 0 && R_vData.size() == 0)
{
for (s = test.begin(); s != test.end(); s++)
{
R_vStr.push_back(*s);
R_vData.push_back(0);
}
}
if(zero > 0 && R_vData.size() > 0)
{
for (s = test.begin()+R_vStr.size() ; s != test.end(); s++)
{
R_vStr.push_back(*s);
R_vData.push_back(0);
}
}
for (s = test.begin(); s != test.end(); s++)
{
if(*s == str)
update = 1;
}
if(update == -1)
test.push_back(str);
for (it = R_vStr.begin(); it != R_vStr.end(); it++)
{
if (*it == str)
return R_vData[it - R_vStr.begin()];
}
R_vStr.push_back(str);
R_vData.push_back(0);
vector<T>::iterator tt = R_vData.end();
tt--;
return *tt;
}
template<typename T>
T Row<T>::operator[]( const string& str ) const
{
vector<string>::iterator it;
for (it = R_vStr.begin(); it != R_vStr.end(); it++)
{
if (*it == str)
return R_vData[it - R_vStr.begin()];
}
R_vStr.push_back(str);
R_vData.push_back(0);
return *(R_vData.end() - 1);
}
template <typename T>
Row<T>& Row<T>::operator=( const Row<T>& s )
{
R_vStr = s.R_vStr;
R_vData = s.R_vData;
return *this;
}
#include "associativematrix.h"
int main()
{
Row<int> M;
M["monday"] = 7;
M["tuesday"] = 8;
M["wednesday"] = 9;
cout << M["monday"] << endl;
cout << M["tuesday"] << endl;
cout << M["wednesday"] << endl;
Row<int>::iterator iter = M.begin();
for(iter = M.begin(); iter != M.end(); ++iter)
cout <<*iter << "";
return 0;
}
Anyway ... now having problem with iterator, wonder if u could help?
The compiler always give me the msg that there isnt iterator template in the std. The code is as follow:
//associativematrix.h
#ifndef associativematrix_h
#define associativematrix_h
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
template <typename T> class AssociativeMatrixIterator;
template <typename T>
class Row
{
public:
friend class AssociativeMatrix<T>;
Row();
Row(const Row& s); // copy constructor
T& operator[]( const string& s );
T operator[]( const string& s ) const;
Row<T>& operator=( const Row<T>& s );
typedef AssociativeMatrixIterator<
iterator begin() { return iterator(R_vData,R_vData.b
iterator end() { return iterator(R_vData,R_vData.b
private:
vector<string> R_vStr;
vector<T> R_vData;
};
template <typename T>
class AssociativeMatrixIterator:
{
public:
typedef T* pointer;
typedef T& reference;
friend class Row<T>;
friend class AssociativeMatrix<T>;
AssociativeMatrixIterator(
: r(&lst), it(i) {}
AssociativeMatrixIterator&
AssociativeMatrixIterator&
AssociativeMatrixIterator operator ++(int);
AssociativeMatrixIterator operator --(int);
reference operator*() const
{
return *it;
}
//template <typename T>
//T* AssociativeIterator<T>::op
pointer operator ->()
{
return &(it);
}
bool operator ==(const AssociativeMatrixIterator&
{
return it == s.it;
}
bool operator !=(const AssociativeMatrixIterator&
{
return !(*this == s );
}
protected:
vector<T>::iterator it;
vector<T>* r;
};
#include "AssociativeMatrix.tem"
#endif
#include "associativematrix.h"
#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<string> test;
template<typename T>
Row<T>::Row() { }
template<typename T>
Row<T>::Row( const Row<T>& s )
{
R_vStr = s.R_vStr;
R_vData = s.R_vData;
}
template<typename T>
T& Row<T>::operator[]( const string& str )
{
int update = -1;
int zero = test.size() - R_vData.size();
/*
cout << "test.size() a : " << test.size() << endl;
cout << "R_vStr.size() a : " << R_vStr.size() << endl;
cout << "R_vData.size() a : " << R_vData.size() << endl;
cout << "zero a : " << zero << endl;
*/
vector<string>::iterator s;
vector<string>::iterator it;
if(zero > 0 && R_vData.size() == 0)
{
for (s = test.begin(); s != test.end(); s++)
{
R_vStr.push_back(*s);
R_vData.push_back(0);
}
}
if(zero > 0 && R_vData.size() > 0)
{
for (s = test.begin()+R_vStr.size()
{
R_vStr.push_back(*s);
R_vData.push_back(0);
}
}
for (s = test.begin(); s != test.end(); s++)
{
if(*s == str)
update = 1;
}
if(update == -1)
test.push_back(str);
for (it = R_vStr.begin(); it != R_vStr.end(); it++)
{
if (*it == str)
return R_vData[it - R_vStr.begin()];
}
R_vStr.push_back(str);
R_vData.push_back(0);
vector<T>::iterator tt = R_vData.end();
tt--;
return *tt;
}
template<typename T>
T Row<T>::operator[]( const string& str ) const
{
vector<string>::iterator it;
for (it = R_vStr.begin(); it != R_vStr.end(); it++)
{
if (*it == str)
return R_vData[it - R_vStr.begin()];
}
R_vStr.push_back(str);
R_vData.push_back(0);
return *(R_vData.end() - 1);
}
template <typename T>
Row<T>& Row<T>::operator=( const Row<T>& s )
{
R_vStr = s.R_vStr;
R_vData = s.R_vData;
return *this;
}
#include "associativematrix.h"
int main()
{
Row<int> M;
M["monday"] = 7;
M["tuesday"] = 8;
M["wednesday"] = 9;
cout << M["monday"] << endl;
cout << M["tuesday"] << endl;
cout << M["wednesday"] << endl;
Row<int>::iterator iter = M.begin();
for(iter = M.begin(); iter != M.end(); ++iter)
cout <<*iter << "";
return 0;
}
#include <iterator> ;)
ASKER
um ... now i have a new problem, i have come out a RowIterator class and a AssociativeMatrixIterator class. By right, the AssociativeMatrixIterator should make use of the RowIterator to access the data in the Row, but somehow it is not working, could you please give some hints ... =)
//associativematrix.h
#ifndef associativematrix_h
#define associativematrix_h
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
template <typename T> class RowIterator;
template <typename T> class AssociativeMatrix;
template <typename T> class AssociativeMatrixData;
template <typename T> class AssociativeMatrixIterator;
template <typename T>
class RowIterator
{
vector<T>::iterator it;
vector<T>* r;
public:
RowIterator() {}
RowIterator(vector<T>& lst):it(0),r(&lst) {}
RowIterator(vector<T>& lst,const typename vector<T>::iterator& i):it(i),r(&lst) {}
bool operator==(const RowIterator& x) const;
bool operator!=(const RowIterator& x) const;
vector<T>::reference operator*() const { return *it; }
RowIterator& operator++();
RowIterator operator++(int);
RowIterator& operator--();
RowIterator operator--(int);
};
template <typename T>
class Row
{
public:
friend class AssociativeMatrix<T>;
friend class AssociativeMatrixData<T>;
friend class AssociativeMatrixIterator< T>;
Row();
Row(const Row& s); // copy constructor
T& operator[]( const string& s );
T operator[]( const string& s ) const;
Row<T>& operator=( const Row<T>& s );
typedef RowIterator<T> riterator;
riterator begin() { return riterator(R_vData, R_vData.begin());}
riterator end() { return riterator(R_vData, R_vData.end()); }
private:
vector<string> R_vStr;
vector<T> R_vData;
};
template <typename T>
class AssociativeMatrix
{
public:
friend class Row<T>;
AssociativeMatrixData<T>* Data;
AssociativeMatrixData<T>* temp;
AssociativeMatrix();
AssociativeMatrix( const AssociativeMatrix& s); // copy constructor
~AssociativeMatrix();
Row<T>& operator[]( const string& s );
Row<T> operator[]( const string& s ) const;
AssociativeMatrix<T>& operator =( const AssociativeMatrix<T>& s );
size_t getRefCount() const;
typedef AssociativeMatrixIterator< T> iterator;
iterator begin() { return iterator(Data->vData, Data->vData.begin()); }
iterator end() { return iterator(Data->vData, Data->vData.end()); }
private:
};
template <typename T>
class AssociativeMatrixData
{
public:
friend class AssociativeMatrix<T>;
int refCount;
vector<string> vStr;
vector<Row<T> > vData;
private:
void increment();
void decrement();
};
template <typename T>
class AssociativeMatrixIterator
{
Row<T>::riterator xt;
vector<Row<T> >* x;
friend class RowIterator<T>;
public:
typedef T* pointer;
//typedef T& reference;
AssociativeMatrixIterator( ) { }
//AssociativeMatrixIterato r(vector<R ow<T> >& st, const typename Row<T>::RowIterator<T>& i) : xt(i), x(&st) {}
AssociativeMatrixIterator( vector<Row <T> >& st, const typename Row<T>::riterator& i) : xt(i), x(&st) {}
bool operator ==(const AssociativeMatrixIterator& s) const;
bool operator !=(const AssociativeMatrixIterator& s) const;
vector<T>::reference operator*() const { return *xt; }
AssociativeMatrixIterator& operator ++();
AssociativeMatrixIterator& operator --();
AssociativeMatrixIterator operator ++(int);
AssociativeMatrixIterator operator --(int);
//template <typename T>
//T* AssociativeIterator<T>::op erator->() { return &(*_currentCell); }
pointer operator ->()
{
return &(xt);
}
};
#include "AssociativeMatrix.tem"
#endif
//associativematrix.h
#ifndef associativematrix_h
#define associativematrix_h
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
template <typename T> class RowIterator;
template <typename T> class AssociativeMatrix;
template <typename T> class AssociativeMatrixData;
template <typename T> class AssociativeMatrixIterator;
template <typename T>
class RowIterator
{
vector<T>::iterator it;
vector<T>* r;
public:
RowIterator() {}
RowIterator(vector<T>& lst):it(0),r(&lst) {}
RowIterator(vector<T>& lst,const typename vector<T>::iterator& i):it(i),r(&lst) {}
bool operator==(const RowIterator& x) const;
bool operator!=(const RowIterator& x) const;
vector<T>::reference operator*() const { return *it; }
RowIterator& operator++();
RowIterator operator++(int);
RowIterator& operator--();
RowIterator operator--(int);
};
template <typename T>
class Row
{
public:
friend class AssociativeMatrix<T>;
friend class AssociativeMatrixData<T>;
friend class AssociativeMatrixIterator<
Row();
Row(const Row& s); // copy constructor
T& operator[]( const string& s );
T operator[]( const string& s ) const;
Row<T>& operator=( const Row<T>& s );
typedef RowIterator<T> riterator;
riterator begin() { return riterator(R_vData, R_vData.begin());}
riterator end() { return riterator(R_vData, R_vData.end()); }
private:
vector<string> R_vStr;
vector<T> R_vData;
};
template <typename T>
class AssociativeMatrix
{
public:
friend class Row<T>;
AssociativeMatrixData<T>* Data;
AssociativeMatrixData<T>* temp;
AssociativeMatrix();
AssociativeMatrix( const AssociativeMatrix& s); // copy constructor
~AssociativeMatrix();
Row<T>& operator[]( const string& s );
Row<T> operator[]( const string& s ) const;
AssociativeMatrix<T>& operator =( const AssociativeMatrix<T>& s );
size_t getRefCount() const;
typedef AssociativeMatrixIterator<
iterator begin() { return iterator(Data->vData, Data->vData.begin()); }
iterator end() { return iterator(Data->vData, Data->vData.end()); }
private:
};
template <typename T>
class AssociativeMatrixData
{
public:
friend class AssociativeMatrix<T>;
int refCount;
vector<string> vStr;
vector<Row<T> > vData;
private:
void increment();
void decrement();
};
template <typename T>
class AssociativeMatrixIterator
{
Row<T>::riterator xt;
vector<Row<T> >* x;
friend class RowIterator<T>;
public:
typedef T* pointer;
//typedef T& reference;
AssociativeMatrixIterator(
//AssociativeMatrixIterato
AssociativeMatrixIterator(
bool operator ==(const AssociativeMatrixIterator&
bool operator !=(const AssociativeMatrixIterator&
vector<T>::reference operator*() const { return *xt; }
AssociativeMatrixIterator&
AssociativeMatrixIterator&
AssociativeMatrixIterator operator ++(int);
AssociativeMatrixIterator operator --(int);
//template <typename T>
//T* AssociativeIterator<T>::op
pointer operator ->()
{
return &(xt);
}
};
#include "AssociativeMatrix.tem"
#endif