error: declaration of 'operator[]' as non-function

Wow, I feel like I'm spamming these boards. Stupid GCC errors.

This is my code(again!):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <vector>
#include <iterator>
#include <algorithm>
#include <exception>

#ifndef __LIST_HPP_INCLUDED
#define __LIST_HPP_INCLUDED

class list_error: public std::exception
{
public:
	virtual const char* what() const throw()
	{
		return errstr;
	}
protected:
	const char* errstr = "";
};

class list_notfound: public list_error
{
protected:
	const char* 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<int> mylist;


The error is referring to here:

 
T operator[] const(int pos)
In this function

1
2
3
4
	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

vect.insert(vect.being(), rhs, rhs + sizeof( rhs ) / sizeof( *rhs ) );
Last edited on
being() was probably also a typo of begin()
Topic archived. No new replies allowed.