function template has already been defined

hello everybody
i am a student who is still moving pretty much his firt steps into c++ programming.
I have been assigned a code for my thesis; the code is correctly compiled under g++. I get lots of errors when trying to compile it with VS. I've jsut created the project and loaded the .cpp and .h files.
After some debugging, there is this problem i can't wrap my head around.
I get this compiling error in the file MappedLinkList.cpp

1>c:\users\francesco\documents\visual studio 2010\projects\minigrid 2011_12_02\files\mappedlinklist.cpp(15): error C2995: 'NS_OCH::MappedLinkList<Key,T>::MappedLinkList(void)' : function template has already been defined
1>          c:\users\francesco\documents\visual studio 2010\projects\minigrid 2011_12_02\files\mappedlinklist.h(13) : see declaration of 'NS_OCH::MappedLinkList<Key,T>::MappedLinkList'


and several more of the same type about the same template function:
1>c:\users\francesco\documents\visual studio 2010\projects\minigrid 2011_12_02\files\mappedlinklist.cpp(20): error C2995: 'NS_OCH::MappedLinkList<Key,T>::~MappedLinkList(void)' : function template has already been defined
1>          c:\users\francesco\documents\visual studio 2010\projects\minigrid 2011_12_02\files\mappedlinklist.h(14) : see declaration of 'NS_OCH::MappedLinkList<Key,T>::~MappedLinkList'
1>c:\users\francesco\documents\visual studio 2010\projects\minigrid 2011_12_02\files\mappedlinklist.cpp(26): error C2995: 'void NS_OCH::MappedLinkList<Key,T>::dump(std::ostream &) const' : function template has already been defined
1>          c:\users\francesco\documents\visual studio 2010\projects\minigrid 2011_12_02\files\mappedlinklist.h(16) : see declaration of 'NS_OCH::MappedLinkList<Key,T>::dump'
1>c:\users\francesco\documents\visual studio 2010\projects\minigrid 2011_12_02\files\mappedlinklist.cpp(33): error C2995: 'void NS_OCH::MappedLinkList<Key,T>::push_front(const Key &,const T &)' : function template has already been defined
1>          c:\users\francesco\documents\visual studio 2010\projects\minigrid 2011_12_02\files\mappedlinklist.h(18) : see declaration of 'NS_OCH::MappedLinkList<Key,T>::push_front'
1>c:\users\francesco\documents\visual studio 2010\projects\minigrid 2011_12_02\files\mappedlinklist.cpp(43): error C2995: 'void NS_OCH::MappedLinkList<Key,T>::erase(const Key &)' : function template has already been defined
1>          c:\users\francesco\documents\visual studio 2010\projects\minigrid 2011_12_02\files\mappedlinklist.h(19) : see declaration of 'NS_OCH::MappedLinkList<Key,T>::erase'
1>c:\users\francesco\documents\visual studio 2010\projects\minigrid 2011_12_02\files\mappedlinklist.cpp(52): error C2995: 'T NS_OCH::MappedLinkList<Key,T>::find(const Key &) const' : function template has already been defined
1>          c:\users\francesco\documents\visual studio 2010\projects\minigrid 2011_12_02\files\mappedlinklist.h(20) : see declaration of 'NS_OCH::MappedLinkList<Key,T>::find'


I have the following code in MappedLinkList.h
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
#include <list>
#include <map>
#include "MappedLinkList.cpp"

namespace NS_OCH {

class OchObject;
template<class Key, class T>
class MappedLinkList: public OchObject {
public:
	MappedLinkList();
	~MappedLinkList();

	virtual void dump(ostream &) const;

	void push_front(const Key&, const T&);	// inserted at the head
	void erase(const Key&);
	T	 find(const Key&) const;

	typename list<T>::iterator begin() {return m_hList.begin();}
	typename list<T>::const_iterator begin() const {return m_hList.begin();}
	typename list<T>::iterator end() {return m_hList.end();}
	typename list<T>::const_iterator end() const {return m_hList.end();}
	typename list<T>::size_type size() const {return m_hList.size();}
	void clear() {m_hList.clear(); m_hMap.clear();}

public:
	list<T>		m_hList;

	typedef pair<Key, typename list<T>::iterator> KeyIteratorPair;
	typedef map<Key, typename list<T>::iterator> KeyIteratorMap;
protected:
	KeyIteratorMap	m_hMap;
};

};


In another file, BinaryHeap.h, the following code is present:
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
#include <vector>
#include "BinaryHeap.cpp"

namespace NS_OCH {

template<class T,
    class Cont = vector<T>,
    class Pred = less<typename Cont::value_type> >
class BinaryHeap {
public:
//    typedef Cont::allocator_type allocator_type;
    typedef typename Cont::value_type value_type;
    typedef typename Cont::size_type size_type;

	explicit BinaryHeap(const Pred& pr = Pred());

    bool empty() const;
    size_type size() const;
	void insert(const value_type&);
	const value_type& peekMin();
	void popMin();
	void buildHeap();

public:
    Cont m_hContainer;
    Pred m_hPredicate;

private:
	void percolateDown(int);
};

};


The problem lies in between these two definitions i guess. I get also this message in the output:
1>c:\users\francesco\documents\visual studio 2010\projects\minigrid 2011_12_02\files\mappedlinklist.cpp(26): error C2244: 'NS_OCH::MappedLinkList<Key,T>::dump' : unable to match function definition to an existing declaration
1>          c:\users\francesco\documents\visual studio 2010\projects\minigrid 2011_12_02\files\mappedlinklist.h(16) : see declaration of 'NS_OCH::MappedLinkList<Key,T>::dump'
1>          definition
1>          'void NS_OCH::MappedLinkList<Key,T>::dump(std::ostream &) const'
1>          existing declarations
1>          'void NS_OCH::MappedLinkList<Key,T>::dump(void) const'


Anyone who can help please?
Thanks a lot in advance
Last edited on
Use include guards in your headers
Topic archived. No new replies allowed.