bizarre compiler errors using templates

I'm a Java dev migrating to C++, so as a practice exercise for learning function pointers and templates, I'm trying to write code for Array classes. I'm using Visual Studio 2012 RC.

I'm getting some really weird compile errors. I can't seem to fix them... I've tried declaring functions that use a T as template functions, but that doesn't solve the problems.

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
#ifndef Matt_Array_h
#define Matt_Array_h
#include <vector>

// tm<cs T> class OrderedGroup: public Array<T>;
// tm<cs T> class Set: public Array<T>;
// tm<cs T> class Array: public iterator<in_iterator_tag, T>;

/* We make no guarantees about the ordering of the elements in this class */
template<class... T> abstract
class Array: public iterator<in_iterator_tag, T>
{
public, virtual:
	/* Array consisting of all the elements of the parm */
	Array( const T [] ) = 0;
	/* Copy constructor. Must be overridden by the implementor. */
	Array( const Array * ) = 0;
	/* Array of given size */
	Array( const int ) = 0;

	int end() const = 0;
	
	/* returns true if parm is true for all T in this */
	bool all(bool(T)) const = 0;
	/* returns true if parm is true for any T in this */
	bool any(bool(T)) const = 0;

	Array<T>& operator += (T []) = 0;
	Array<T>& operator += (Array<T>) = 0;
	Array<T>& operator += (T) = 0;
	
	Array<T>& operator << (T [] p) {
		return this += p;
	}
	Array<T>& operator << (Array<T> p) {
		return this += p;
	}
	Array<T>& operator << (T p) {
		return this += p;
	}

	/* set each T in this to the parm */
	void fill(&T) const = 0;

	/* apply the parm to each T in this */
	void each( void(T) ) const; // Error at "void": A parameter may not have a void type
                                    // Error at "const": A type qualifier is not allowed on a non-member function

	/* return an Array<T> with all elements for which calling the parm on that element returned false */
	Array<T>& filter( bool(T) ) const; // Error at "Array": Array is not a template
                                           // Error at "T": identifier "T" is undefined
                                           // Error at "const": A type qualifier is not allowed on a non-member function



	std::vector<T> toVector() const; // Error at "T": identifier "T" is undefined
                                         // Error at "const": A type qualifier is not allowed on a non-member function
	template <class V> V fold(&V, V(T)) const;
}; // Error at "}": Expected a declaration 
Last edited on
I don't think the compiler errors are bizarre - it's this code that is bizarre.
template<class... T>
The dots here suggests you are trying to write a variadic template (taking any number of template arguments) but I don't think that is what you want so just remove the dots.

abstract
There is no abstract keyword in C++. If you want to make it impossible to create instances of the class you can have at least one pure virtual function or make the constructors protected.

class Array: public iterator<in_iterator_tag, T>
It should be std::iterator but an array isn't an iterator so it doesn't make sense to inherit from it.

public, virtual:
Remove , virtual from there and put virtual in front of all the virtual functions instead.
virtual int end() const = 0;

virtual void fill(&T) const = 0;
If this function is supposed to take a reference it should be virtual void fill(T&) const = 0;

Array<T>& operator << (T [] p) {
[] is on the wrong side of p.

Array<T>& operator += (Array<T>) = 0;
Since the class is abstract you can't pass Array objects by value because that would require an Array object to be created. You have to pass it by reference (or pointer).

You also need #endif at the end of the file to match the opening #ifndef .
Last edited on
Can't have virtual constructors either.

also - if Array is an abstract class then
you cannot create object of type Array<T> - this mesans that
some of your functions like the operator += function is incorrect.
Last edited on
Thanks so much. I hesitated at "abstract" but VS bolded it, so I thought it existed.

I think I need to figure out how polymorphism works a little better. I inherited from iterator so that I could pass an Array& to a function that expected an iterator.

I suppose the only thing I can say in my defense is that I did have an #endif at the end but I elided it :P

guestgulkan, is the operator+= error(s, knowing me) that += (Array<T>) fixed by += (const Array<T>&)?
Last edited on
Topic archived. No new replies allowed.