- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试使用类模板实现我自己的 OrderedList 数据结构。我的 CPP 文件中的每个构造函数和函数在开头“{”时都有错误。错误显示为“重新定义 FUNCTION_NAME FUNTION_NAME previously declared here”。
我让一些同事看过它,但无济于事。这是我的文件和错误:
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
Node.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
最佳答案
您的问题是您在 header 中包含函数的定义(间接通过 #include "MyOrderedList.cpp"
。因为函数未声明为 inline
编译器在包含 header 的所有翻译单元中生成符号,因此您会得到多重定义的符号。
该问题的一个解决方案是在定义中将函数声明为 inline
,以便链接器可以丢弃重复项。将定义保存在单独的文件中并不是什么新鲜事,但也不太常见,一些标准库实现就是这样做的。但是请考虑将 .cpp 重命名为其他名称以避免混淆(大多数人会认为 .cpp 意味着它是要编译的,而不是包含在内)。
另一方面,我建议将其进一步简化,完全删除 .cpp 并只包含所有内容的 .h(仍将所有函数定义标记为 inline
),甚至更进一步,在类定义中定义函数,默认情况下它们是 inline
。
关于c++ - 编译器说每个构造函数和函数都被重新定义并且不会编译。我使用的#IFNDEF 错了吗? - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10165415/
我无法在附加行中显示“真”、“假”、"is"和“否”按钮。 我在这里有一个应用程序:Application 请按照以下步骤使用应用程序: 1。当你打开应用程序时,你会看到一个绿色的加号按钮,点击 在此
我是一名优秀的程序员,十分优秀!