Error C2065 & C2955

1
2
3
4
5
6
7
8
9
10
#include "iterator.h"
#include "genlib.h"
#include <string>
using namespace std;

template <typename ElemType>
Iterator<ElemType>::Iterator()
{
    start = tail = NULL;
}
And those errors are?

In any case, I would hazard to guess you'll probably need to show more code than just this.
error C2995: 'Iterator<ElemType>::Iterator(void)' : function template has already been defined

C2065 is not showing now

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef _iterator_h
#define _iterator_h

/*
 * Class: Iterator
 * ---------------
 * This class is used to iterate over a collection of elements.
 */

template <typename ElemType>
class Iterator {
 public:
/*
 * Constructor: Iterator
 * Usage: iter = new Iterator<double>();
 * ------------------------------------
 * The constructor allocates a new iterator with no entries.
 */
Iterator();


1
2
3
4
5
6
7
8
9
10
#include "iterator.h"
#include "genlib.h"
#include <string>
using namespace std;

template <typename ElemType>
Iterator<ElemType>::Iterator()
{
    start = tail = NULL;
}
Last edited on
I think that the constructor was already defined in the header file.

P.S. As for expression

Error C2065 & C2955

the result will be

Error C2045 :)
Last edited on
what does 'defined' mean?

It means that something as this code

1
2
3
4
5
template <typename ElemType>
Iterator<ElemType>::Iterator()
{
    start = tail = NULL;
}


already is present in the header.

nope - nothing like that, just the function
Iterator();

and class template template <typename ElemType>

This compiled in VS2005 at one time, now in 2008 and 2011 there are a bunch of these errors
Last edited on
Show your header file.
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
/*
 * File: iterator.h
 * CS106 Section Leader: Alan Viverette 
 * Last modified on Fri June 8 09:50:00 2012 by granite
 *
 * -----------------------------------------------------
 * This file implements an iterator class template.
 * You should not need to modify this file.
 */

#ifndef _iterator_h
#define _iterator_h

/*
 * Class: Iterator
 * ---------------
 * This class is used to iterate over a collection of elements.
 */

template <typename ElemType>
class Iterator {

 public:
/*
 * Constructor: Iterator
 * Usage: iter = new Iterator<double>();
 * ------------------------------------
 * The constructor allocates a new iterator with no entries.
 */
Iterator();

/*
 * Destructor: ~Iterator
 * Usage: delete iter;
 * --------------------
 * The destructor frees the storage associated with the 
 * iterator.
 */
~Iterator();

 /*
 * Member function: hasNext
 * Usage: if (iter.hasNext())...
 * ------------------------------
 * This function returns true until all the values are
 * exhausted, after which it returns false.
 */
bool hasNext();

/*
 * Member function: next
 * Usage: value = iter.next();
 * ----------------------------
 * This function advances the iterator and returns the
 * the next value. An Error is raised if next is called
 * when hasNext would return false.
 */
ElemType next();

/*
 * Member function: add
 * Usage: iter.add(elem);
 * ------------------------------
 * This function adds a new element to the iterator. It is
 * intended to be called by the container when initializing
 * the iterator to hold the collection values to be
 * iterated over.
 */
void add(ElemType elem);

private: 

  struct cellT {
    ElemType elem;
    cellT *link;
  };

  cellT *start, *tail;
};

#include "iterator.cpp"

#endif 
I think the problem is in this statement

#include "iterator.cpp"

You already included the definition of the constructor due to this satement. And after that you again include it in the code

#include "iterator.h" <== This includes "iterator.cpp"
#include "genlib.h"
#include <string>
using namespace std;

template <typename ElemType>
Iterator<ElemType>::Iterator()
{
start = tail = NULL;
}

You shall all definition of your template class transfer to the header file. So you will have one header file in which there will be the class declaration and its definitions.
Last edited on
so... confusion follows and the compiler becomes confused, then I ma confused, but since the compiler was already confused then I simply was already confused?
You have file circularity.
I have confusion circularity, but nothing a beer and //#include "iterator.cpp" can't fix
As I said you should all definitions of your class to place into the same header file. So you will have one header file that will contain all definitions of your template class.
Topic archived. No new replies allowed.