bundle of errors

These errors seem to ask for a header file but it is not logical that x.cpp needs to reference x.h. I am chasing a circle of adding a .h file and having already referenced errors and removing the .h and finding this bundle off errors.

C2143: syntax error : missing ';' before '<'

C4430: missing type specifier - int assumed. Note: C++ does not support default-int

C2988: unrecognizable template declaration/definition

C2059: syntax error : '<'

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * File: private/set.cpp
 * Last modified on Thu Jun 11 09:34:08 2009 by eroberts
 * -----------------------------------------------------
 */

//#ifdef _set_h //original code

#ifndef _set_h
#define _set_h


#include "stdafx.h"


template <typename ElemType>
Set<ElemType>::Set(int (*cmp)(ElemType, ElemType)) : bst(cmp) {
	cmpFn = cmp;
}

template <typename ElemType>
Set<ElemType>::~Set() {
	/* Empty */
}

template <typename ElemType>
int Set<ElemType>::size() {
	return bst.size();
}

template <typename ElemType>
bool Set<ElemType>::isEmpty() {
	return bst.isEmpty();
}

template <typename ElemType>
void Set<ElemType>::add(ElemType element) {
	bst.add(element);
}

template <typename ElemType>
void Set<ElemType>::remove(ElemType element) {
	bst.remove(element);
}

template <typename ElemType>
bool Set<ElemType>::contains(ElemType element) {
	return find(element) != NULL;
}

template <typename ElemType>
ElemType *Set<ElemType>::find(ElemType element) {
	return bst.find(element);
}

template <typename ElemType>
void Set<ElemType>::clear() {
	bst.clear();
}

/*
 * Implementation notes: Set operations
 * ------------------------------------
 */

template <typename ElemType>
bool Set<ElemType>::equals(Set & otherSet) {
	if (cmpFn != otherSet.cmpFn) {
		Error("Equals: sets have different comparison functions");
	}
	Iterator thisItr = iterator(), otherItr = otherSet.iterator();
	while (thisItr.hasNext() && otherItr.hasNext()) {
		if (cmpFn(thisItr.next(), otherItr.next()) != 0) return false;
	}
	return !thisItr.hasNext() && !otherItr.hasNext();
}

template <typename ElemType>
bool Set<ElemType>::isSubsetOf(Set & otherSet) {
	if (cmpFn != otherSet.cmpFn) {
		Error("isSubsetOf: sets have different comparison functions");
	}
	Iterator iter = iterator();
	while (iter.hasNext()) {
		if (!otherSet.contains(iter.next())) return false;
	}
	return true;
}

template <typename ElemType>
void Set<ElemType>::unionWith(Set & otherSet) {
	if (cmpFn != otherSet.cmpFn) {
		Error("unionWith: sets have different comparison functions");
	}
	Iterator iter = otherSet.iterator();
	while (iter.hasNext()) {
		add(iter.next());
	}
}

/*
 * Implementation notes: intersectWith
 * -----------------------------------
 */

template <typename ElemType>
void Set<ElemType>::intersectWith(Set & otherSet) {
	if (cmpFn != otherSet.cmpFn) {
		Error("intersectWith:"
		      " sets have different comparison functions");
	}
	Iterator iter = iterator();
	Vector<ElemType> toDelete;
	while (iter.hasNext()) {
		ElemType elem = iter.next();
		if (!otherSet.contains(elem)) toDelete.add(elem);
	}
	for (int i = 0; i < toDelete.size(); i++) {
		remove(toDelete[i]);
	}
}

template <typename ElemType>
void Set<ElemType>::intersect(Set & otherSet) {
	if (cmpFn != otherSet.cmpFn) {
		Error("intersect: sets have different comparison functions");
	}
	intersectWith(otherSet);
}

template <typename ElemType>
void Set<ElemType>::subtract(Set & otherSet) {
	if (cmpFn != otherSet.cmpFn) {
		Error("subtract: sets have different comparison functions");
	}
	Iterator iter = otherSet.iterator();
	while (iter.hasNext()) {
		remove(iter.next());
	}
}

template <typename ElemType>
void Set<ElemType>::mapAll(void (*fn)(ElemType)) {
	bst.mapAll(fn);
}

template <typename ElemType>
template <typename ClientDataType>
void Set<ElemType>::mapAll(void (*fn)(ElemType, ClientDataType &),
                           ClientDataType & data) {
	bst.mapAll(fn, data);
}

/*
 * Set::Iterator class implementation
 * ----------------------------------
 */

template <typename ElemType>
Set<ElemType>::Iterator::Iterator() {
	/* Empty */
}

template <typename ElemType>
typename Set<ElemType>::Iterator Set<ElemType>::iterator() {
	return Iterator(this);
}

template <typename ElemType>
Set<ElemType>::Iterator::Iterator(Set *setptr) {
	iterator = setptr->bst.iterator();
}

template <typename ElemType>
bool Set<ElemType>::Iterator::hasNext() {
	return iterator.hasNext();
}

template <typename ElemType>
ElemType Set<ElemType>::Iterator::next() {
	return iterator.next();
}

template <typename ElemType>
ElemType Set<ElemType>::foreachHook(FE_State & fe) {
	if (fe.state == 0) fe.iter = new Iterator(this);
	if (((Iterator *) fe.iter)->hasNext()) {
		fe.state = 1;
		return ((Iterator *) fe.iter)->next();
	} else {
		fe.state = 2;
		return ElemType();
	}
}



#endif 


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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * File: set.h
 * Last modified on Thu Jun 11 09:17:43 2009 by eroberts
 *      modified on Tue Jan  2 14:34:06 2007 by zelenski
 * -----------------------------------------------------
 * This interface file contains the Set class template, a
 * collection for efficiently storing a set of distinct elements.
 */

#ifndef _set_h
#define _set_h

#include "cmpfn.h"
#include "bst.h"
#include "vector.h"
#include "foreach.h"


/*
 * Class: Set
 * ----------
 */

template <typename ElemType>
class Set {

public:

/* Forward references */
	class Iterator;

/*
 * Constructor: Set
 * Usage: Set<int> set;
 *        Set<student> students(CompareStudentsById);
 *        Set<string> *sp = new Set<string>;
 * -----------------------------------------
 */
	Set(int (*cmpFn)(ElemType, ElemType) = OperatorCmp);

/*
 * Destructor: ~Set
 * Usage: delete sp;
 * -----------------
 * The destructor deallocates  storage associated with set.
 */
	~Set();

/*
 * Method: size
 * Usage: count = set.size();
 * --------------------------
 * This method returns the number of elements in this set.
 */
	int size();

/*
 * Method: isEmpty
 * Usage: if (set.isEmpty())...
 * ----------------------------
 * This method returns true if this set contains no
 * elements, false otherwise.
 */
	bool isEmpty();

/*
 * Method: add
 * Usage: set.add(value);
 * ----------------------
 */
	void add(ElemType elem);

/*
 * Method: remove
 * Usage: set.remove(value);
 * -----------------------
 */
	void remove(ElemType elem);

/*
 * Method: contains
 * Usage: if (set.contains(value))...
 * -----------------------------------
 * Returns true if the element in this set, false otherwise.
 */
	bool contains(ElemType elem);

/*
 * Method: find
 * Usage: eptr = set.find(elem);
 * -----------------------------
 */
	ElemType *find(ElemType elem);

/*
 * Method: equals
 * Usage: if (set.equals(set2)) . . .
 * -----------------------------------
 */
	bool equals(Set & otherSet);

/*
 * Method: isSubsetOf
 * Usage: if (set.isSubsetOf(set2)) . . .
 * --------------------------------------
 */
	bool isSubsetOf(Set & otherSet);

/*
 * Methods: unionWith, intersectWith, subtract
 * Usage: set.unionWith(set2);
 *        set.intersectWith(set2);
 *        set.subtract(set2);
 * -------------------------------
 */
	void unionWith(Set & otherSet);
	void intersectWith(Set & otherSet);
	void subtract(Set & otherSet);

/*
 * Method: clear
 * Usage: set.clear();
 * -------------------
 */
	void clear();

/*
 * SPECIAL NOTE: mapping/iteration support
 * ---------------------------------------
 */

/*
 * Method: mapAll
 * Usage: set.mapAll(Print);
 * -------------------------
 * This method iterates through this set's contents
 * and calls the function fn once for each element.
 */
	void mapAll(void (*fn)(ElemType elem));

/*
 * Method: mapAll
 * Usage: set.mapAll(PrintToFile, outputStream);
 * --------------------------------------------
 */
	template <typename ClientDataType>
	void mapAll(void (*fn)(ElemType elem, ClientDataType & data),
	            ClientDataType & data);

/*
 * Method: iterator
 * Usage: iter = set.iterator();
 * -----------------------------
 * To avoid exposing the details of the class, the definition of the
 * Iterator class itself appears in the private/set.h file.
 */
	Iterator iterator();

private:

#include "private/set.h"

};

#include "private/set.cpp"

#endif
Maybe I haven't learned it yet, but why do you have header guards in a .cpp?

Especially with the same exact names as your header file header guards? It should give you errors, unless I'm mistaken.
I did not notice that - and good question. The code had #ifdef _set_h, it resulted in grayed out text, I did not understand what it did so looked it up and changed to the ifndef that you saw.

Removing the header guard did not resolve the errors.
If you're using code blocks, there is an option to change that. I'll look it up. As for your stdafx.h, do you include the set.h as well?
no, there is not a set.h header - shouldn't the .cpp file automatically associate with the .h file and if it is referenced will cause a circular reference
ummm no...

set.cpp should have this at the beginning:
1
2
3
#include "set.h"

#ifdef _set_h 


The reason being is that because set.h is pasted into set.cpp at the very beginning, _set_h will have been defined. The ifdef preprocessor directive will see that _set_h is already defined and include the code at the bottom. Also, make sure you have all of the #include "..." files from set.h or else you'll never be able to compile it.

Edit: If you're using Code::Blocks, to turn off the gray out option for #if's, go to:
Settings -> Editor -> General settings -> Other options -> Interpret #if, #else, and #endif to grey out inactive preprocessor-code
Last edited on
so that resolves those errors but causes a couple dozen C2995 : function template has already been defined

I have read conflicting instruction on placing a .h reference in the .cpp file.
It's probably because this code was written three years ago. I see it #includes foreach which is now standard. A lot of the stuff could just be obsolete. Maybe if you tried running it with an old compiler like that that is included with bloodshed, you'd be able to get it to run. Question is, why do you need it?
to run code I wrote several years ago
With all the new updates and standards coming out, I wouldn't be surprised if the code has become obsolete. That's the only thing I can think of since I believe the code worked when it was last used, why else would it stop working?
Last edited on
a class library was installed when I last ran the code and I have since changed hardrives and changed from VS2005 to VS2008; I reassembled the library from the individual class files
With the update from 2005 to 2008 there could have also been a lot of changes in the compilers. As for your errors you're getting, it's hard to know everything thats going on since I can't compile the code, your errors aren't very descriptive as from where they're coming from, or what template they're referring to.

Sorry I can't be of more help to you.
Template functions cannot be separated into a .cpp/.hpp file. They need to all be in the header file.
I did some research and figured out that the private/set.cpp file is treated like a .h file so it is the same as if the function is in a single header file
Topic archived. No new replies allowed.