为了账号安全,请及时绑定邮箱和手机立即绑定

请问为什么编译器提示redefinition?高手来帮忙

请问为什么编译器提示redefinition?高手来帮忙

PIPIONE 2021-12-16 11:11:06
min_heap.h#ifndef MIN_HEAP_H#define MIN_HEAP_H#include <vector>using namespace std;template <class T>class min_heap{private:vector<T> v;int Parent (int index);int LeftChild (int index);int RightChild (int index);void Heapify (vector<T> vec, int index);void Build_Heap();public:min_heap ();void Insert (T value);T ExtractMin ();T Minimum ();void PrintHeap ();};#include "min_heap.cpp"#endif // MIN_HEAP_H#include "min_heap.h"#include <iostream>using namespace std;template <class T>min_heap<T>::min_heap (){v.resize(0);}template <class T>int min_heap<T>::Parent (int index){if (index%2 == 0)return (index - 1)/2;elsereturn index / 2;}template <class T>int min_heap<T>::LeftChild (int index){return 2 * index + 1;}template <class T>int min_heap<T>::RightChild (int index){return 2 * index + 2;}template <class T>void min_heap<T>::Heapify (vector<T> vec, int index){int left = LeftChild (index);int right = RightChild (index);int smallest;T temp;if (left <= vec.size() && vec[left] < vec[index])smallest = left;elsesmallest = index;if (right <= vec.size() && vec[right] < vec[smallest])smallest = right;if (smallest != index){temp = vec[index];vec[index] = vec[smallest];vec[smallest] = temp;Heapify(vec, smallest);}}template <class T>void min_heap<T>::Build_Heap(){for (int i = (v.size()/2 - 1); i >= 0; i--)Heapify(v, i);}template <class T>void min_heap<T>::Insert (T value){v.push_back(value);int i = v.size() - 1;while (i > 0 && v[Parent(i)] < value){v[i] = v[Parent(i)];i = Parent(i);}v[i] = value;}template <class T>T min_heap<T>::ExtractMin (){if (v.size() < 1)cout << "Heap underflow" << endl;else{int smallest = v[0];v[0] = v[v.size() - 1];v.pop_back();Heapify(v, 0);return smallest;}}template <class T>T min_heap<T>::Minimum (){}错误如下:...min_heap.cpp|6|error: redefinition of 'min_heap<T>::min_heap()'|...min_heap.cpp|6|error: 'min_heap<T>::min_heap()' previously declared here|一共20个错误都是这样的
查看完整描述

2 回答

?
开心每一天1111

TA贡献1836条经验 获得超13个赞

#include "min_heap.cpp"
#endif // MIN_HEAP_H

改成
static min_heap<int> min_heap_int; // generates the constructor and destructor code
#endif // MIN_HEAP_H

这是下面的第4种解决方法,你可以再试试其它的

----------------------------------------------------------------------------------------------------------------
一般类的定义和实现组织方式:将定义放在.h文件中,实现放在.cpp文件中。

C++中的模板是C++在发展过程中新添的新生力量,template的声明和实现不同于一般的类定义与实现和函数的声明与定义,故在含有模板的C++程序的组织方式就不同于一般的定义与实现相分离的方式。

我曾经遇到的问题:1. 当在GUN中编译时老是无法通过,错误信息大概是无法找到函数。然后将程序搬到VC6下编译,同样出现连接错误。
2.
将类的定义放在一个头文件中,而类的实现在另一个.cpp文件里,程序代码(main函数)放在单独的一个.cpp文件下,在vc2008下编译会出现如类的某某函数被重新定义之类的错误。
问题原因:

C++标准中说明:在使用模板时C++支持两种程序组织方式:包含模式与分离模式。包含模式也就是将类的定义与实现同放在.h
文件中,分离模式也就是将定义与实现分离,也就是我采用的方式。但很多编译器不支持分离模式,只支持包含模式。

通常采用的解决方法:

1. 干脆直接使用包含模式,即将模板类的定义与实现同写在.h文件(.h文件应用双引号而不是<>)中。(我不太喜欢这种写法)

2.
使用分离模式,但是在使用时不引用模板类的头文件,而是引用模板类的实现文件。(不太符合一般习惯)

3.
使用分离模式,在模板类头文件中引用实现文件。(这个方法我在VC6和Dev C++下都没有成功,不知道是哪里出了问题)

4.
使用分离模式,在模板类中头文件中实例化一个你需要对象。(十分笨拙的方法,不利于使用)

5.
使用分离模式,但是另外定义.h文件,在这个文件中引用模板的头文件和实现文件,在使用时引用这个另外定义的.h文件。(觉得这个方法还不错,不过也不知道有什么缺点)



查看完整回答
反对 回复 2021-12-19
?
撒科打诨

TA贡献1934条经验 获得超2个赞

模板类是不支持声明与实现细节分离的。它不像一般的源代码,因为实例化模板的时候编译器并不知道在哪去找声明与实现。

查看完整回答
反对 回复 2021-12-19
  • 2 回答
  • 0 关注
  • 483 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信