3

我正在尝试使用类模板实现我自己的 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
4

3 回答 3

4

您的问题是您在标头中包含函数的定义(间接通过#include "MyOrderedList.cpp". 因为未声明函数,因为inline编译器在包含标头的所有翻译单元中生成符号,因此您得到多重定义的符号。

该问题的一种解决方案是inline在定义中声明函数,以便链接器可以丢弃重复项。将定义保存在单独的文件中并不是什么新鲜事,但也不是很常见,一些标准库实现会这样做。但是考虑将 .cpp 重命名为其他名称以避免混淆(大多数人会认为 .cpp 意味着它应该被编译,而不是包含在内)。

另一方面,我建议将其简化一点,完全删除 .cpp 并仅将 .h 与所有内容一起使用(仍将所有函数定义标记为inline),甚至更进一步并在类中定义函数定义,默认情况下它们在哪里inline

于 2012-04-15T20:09:10.633 回答
2

.cpp.h对方#include归档。为什么?

于 2012-04-15T19:46:32.637 回答
0

模板类的声明和定义需要保持在一起。为了做到这一点,#include在您的文件上使用并没有错,但是您的文件还需要标头保护(即等)。然而,通常的方法是将所有内容都推到同一个文件中 .cpp.cpp#ifndef MYORDEREDLIST_CPP.h

注意 - 模板类/函数是您唯一需要#include的 .cpp 文件或将定义放入头文件中。

于 2012-04-15T19:58:19.243 回答