error: expected primary-expression before ';' token

I have this code:

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
#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;
	}
	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
C:\C++\include/list.hpp: In member function 'int list<T>::index(T)':
C:\C++\include/list.hpp:84:22: error: expected primary-expression before ';' token


Line 84 is:

 
throw list_notfound;
list_notfound is a type. You don't throw a type, you throw an instance of a type. Create an object of that class type, and throw that.

If you're confused, there's a tutorial about exceptions on this site.
You have to throw objects... you cannot throw a class name.

list_notfound is a class -- a type. throw list_notfound; is like doing throw int;.

You need to create an object of that type, and throw the object.

This can be done by invoking the ctor, rather than just giving the class name:

 
throw list_notfound(); // <- now you're throwing an object of that type 



EDIT:

ninja'd by MikeyBoy
Last edited on
Topic archived. No new replies allowed.