Link to home
Start Free TrialLog in
Avatar of Bottom
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 ==(AssociativeMatrixIterator& s)
            {
                        return this == s;
            }

            bool operator !=(AssociativeMatrixIterator& 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>::AssociativeMatrix()
{
      Data = new AssociativeMatrixData<T>();
      Data -> refCount = 1;
}

template<typename T>
AssociativeMatrix<T>::AssociativeMatrix( const AssociativeMatrix& s )
{
      Data = s->Data;
      Data->increment();
}

template<typename T>
Row<T>& AssociativeMatrix<T>::operator[]( 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>::operator[]( 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>::operator =( const AssociativeMatrix<T>& s )
{
      Data->decrement();
      Data = s->Data;
      Data->increment();

      return *this;
}

template <typename T>
size_t AssociativeMatrix<T>::getRefCount() 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;
}
Avatar of anovickis
anovickis

builds OK in VS7/VC++

Avatar of Bottom

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;
}
I'm looking
Try making an instance of
AssociativeMatrixIterator<float> iZ;
that's the first problem

Avatar of Bottom

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
Avatar of Bottom

ASKER

anybody having any ides how can i access to every single elements of the Matrix from operator []?
ASKER CERTIFIED SOLUTION
Avatar of akunite
akunite

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bottom

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.begin()); }

            iterator end() { return iterator(R_vData,R_vData.begin()); }


      private:

            vector<string> R_vStr;
            vector<T> R_vData;            
};

template <typename T>
class AssociativeMatrixIterator:public std::iterator<std::bidirectional_iterator_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>::operator->() { 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;
}

#include <iterator> ;)
Avatar of Bottom

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() { }

            //AssociativeMatrixIterator(vector<Row<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>::operator->() { return &(*_currentCell); }

            pointer operator ->()
            {
                  return &(xt);
            }
};



#include "AssociativeMatrix.tem"
#endif