我正在尝试使用类模板实现我自己的 OrderedList 数据结构。我的 CPP 文件中的每个构造函数和函数在开头的“{”上都有一个错误。错误显示为“重新定义之前在此处声明的 FUNCTION_NAME FUNTION_NAME”。
我已经让几个同事看过它,但无济于事。这是我的文件和错误:
CPP 文件“MyOrderedList.cpp”
#include "MyOrderedList.h"
template <class E>
MyOrderedList<E>::MyOrderedList() {/*IMPLEMENTATION*/}
template <class E>
MyOrderedList<E>::MyOrderedList(const MyOrderedList<E>& orig) { /*IMPLEMENTATION*/}
template <class E>
void MyOrderedList<E>::operator =(const MyOrderedList<E>& orig){/*IMPLEMENTATION*/}
template <class E>
MyOrderedList<E>::~MyOrderedList() {/*IMPLEMENTATION*/}
template <class E>
void MyOrderedList<E>::insert(E data){/*IMPLEMENTATION*/}
template <class E>
E MyOrderedList<E>::get(int pos) const{/*IMPLEMENTATION*/}
template <class E>
Node<E>* MyOrderedList<E>::getHead() const{/*IMPLEMENTATION*/}
template <class E>
MyOrderedList<E> MyOrderedList<E>::kLargest(int k) const{/*IMPLEMENTATION*/}
template <class E>
MyOrderedList<E> MyOrderedList<E>::operator +(const MyOrderedList<E>& list {/*IMPLEMENTATION*/}
H 文件“MyOrderedList.h”
#ifndef MYORDEREDLIST_H
#define MYORDEREDLIST_H
#include <iostream>
#include <cstdlib>
#include "Node.h"
template <class E>
class MyOrderedList;
template <class E>
std::ostream& operator <<(std::ostream& out, const MyOrderedList<E>& list);
template <class E>
class MyOrderedList {
public:
MyOrderedList();
MyOrderedList(const MyOrderedList<E>& orig);
void operator =(const MyOrderedList<E>& orig);
virtual ~MyOrderedList();
bool remove(E data);
MyOrderedList<E> kLargest(int k) const;
E get(int pos) const;
void insert(E data);
Node<E>* getHead() const;
MyOrderedList<E> operator +(const MyOrderedList<E>& list);
friend std::ostream& operator <<(std::ostream& out, const MyOrderedList<E>& list){/*IMPLEMENTATION*/}
private:
Node<E>* head;
int size;
};
#include "MyOrderedList.cpp"
#endif //MYORDEREDLIST_H
节点.h
#ifndef NODE_H
#define NODE_H
#include <iostream>
template <class E>
class Node {
public:
Node(const E& init_data = NULL, Node<E>* init_link = NULL){data = init_data; link = init_link;}
Node(const Node<E>& orig);
virtual ~Node();
E getData() const{return data;}
void setData(E newData);
Node<E>* getLink(){return link;}
void setLink(Node<E>* nextLink) {link = nextLink;}
private:
E data;
Node<E>* link;
};
#endif /* NODE_H */
编译器错误
MyOrderedList.cpp:12: error: redefinition of `MyOrderedList<E>::MyOrderedList()'
MyOrderedList.cpp:12: error: `MyOrderedList<E>::MyOrderedList()' previously declared here
MyOrderedList.cpp:19: error: redefinition of `MyOrderedList<E>::MyOrderedList(const MyOrderedList<E>&)'
MyOrderedList.cpp:19: error: `MyOrderedList<E>::MyOrderedList(const MyOrderedList<E>&)' previously declared here
MyOrderedList.cpp:42: error: redefinition of `void MyOrderedList<E>::operator=(const MyOrderedList<E>&)'
MyOrderedList.cpp:42: error: `void MyOrderedList<E>::operator=(const MyOrderedList<E>&)' previously declared here
MyOrderedList.cpp:77: error: redefinition of `MyOrderedList<E>::~MyOrderedList()'
MyOrderedList.cpp:77: error: `virtual MyOrderedList<E>::~MyOrderedList()' previously declared here
MyOrderedList.cpp:91: error: redefinition of `void MyOrderedList<E>::insert(E)'
MyOrderedList.cpp:91: error: `void MyOrderedList<E>::insert(E)' previously declared here
MyOrderedList.cpp:119: error: redefinition of `E MyOrderedList<E>::get(int) const'
MyOrderedList.cpp:119: error: `E MyOrderedList<E>::get(int) const' previously declared here
MyOrderedList.cpp:134: error: redefinition of `Node<E>* MyOrderedList<E>::getHead() const'
MyOrderedList.cpp:134: error: `Node<E>* MyOrderedList<E>::getHead() const' previously declared here
MyOrderedList.cpp:140: error: redefinition of `MyOrderedList<E> MyOrderedList<E>::kLargest(int) const'
MyOrderedList.cpp:140: error: `MyOrderedList<E> MyOrderedList<E>::kLargest(int) const' previously declared here
MyOrderedList.cpp:158: error: redefinition of `MyOrderedList<E> MyOrderedList<E>::operator+(const MyOrderedList<E>&)'
MyOrderedList.cpp:158: error: `MyOrderedList<E> MyOrderedList<E>::operator+(const MyOrderedList<E>&)' previously declared here