#include <vector>
#include <iterator>
#include <algorithm>
#include <exception>
#ifndef __LIST_HPP_INCLUDED
#define __LIST_HPP_INCLUDED
class list_error: public std::exception
{
public:
virtualconstchar* what() constthrow()
{
return errstr;
}
protected:
constchar* errstr = "";
};
class list_notfound: public list_error
{
protected:
constchar* errstr = "The specified element could not be found";
};
template <typename T>
class list
{
public:
list() {};
~list() {}
list operator=(T rhs[])
{
vect.insert(vect.being(), rhs, sizeof(rhs)); return *this;
}
T operator[] const(int pos)
{
return vect[pos];
}
T& operator[](int pos)
{
return vect[pos];
}
void append(T item)
{
vect.push_back(item);
}
void insert(int position, T item)
{
vect.insert(vect.begin() + position, item);
}
int count(T item)
{
int total = 0;
for (iter = vect.begin(); iter != vect.end(); ++iter)
{
if (*iter == item)
{
total++;
}
}
return total;
}
void reverse()
{
std::reverse(vect.begin(), vect.end());
}
T pop(int element=-1)
{
int pos;
if (element == -1)
{
pos = vect.size() - 1;
}
else
{
pos = element;
}
T &ret = vect.at(pos);
vect.erase(vect.begin()+pos);
return ret;
}
int index(T item)
{
for (iter = vect.begin(); iter != vect.end(); ++iter)
{
if (*iter == item)
{
return *iter;
}
}
throw list_notfound();
}
private:
std::vector<T> vect;
typename std::vector<T>::const_iterator iter;
};
#endif
And GCC is giving me this error:
1 2 3 4
C:\C++\include/list.hpp:36:13: error: declaration of 'operator[]' as non-function
C:\C++\include/list.hpp:36:13: error: expected ';' at end of member declaration
C:\C++\include/list.hpp:36:21: error: expected unqualified-id before 'int'
C:\C++\include/list.hpp:36:21: error: expected ')' before 'int'
I have researched this like crazy to no avail. I am calling the class right:
list operator=(T rhs[])
{
vect.insert(vect.being(), rhs, sizeof(rhs)); return *this;
}
you have two errors. The first one there is no such member function as being. The second one you should use sizeof( rhs ) / sizeof( *rhs ) instead of sizeof( rhs )
The operator [] is declared incorrectly
1 2 3 4
T operator[] const(int pos)
{
return vect[pos];
}
Shall be
1 2 3 4
T operator[] (int pos) const
{
return vect[pos];
}
Edit: the first function contains even more errors. There is no such member function as your function insert in class std:;vector.:) Maybe you meant