c++重定义错误

C++ Redefinition Error

本文关键字:错误 定义 c++      更新时间:2023-10-16

我已经寻找了这个问题的答案,并尝试了建议的解决方案。即#pragma once#ifndef/#endif

我遇到的问题是,我的代码中的每个函数都被多次定义。我使用#pragma试图减少头包含到一次,我作为一般做法,我总是在我的头文件中包括#ifndef/#endif。这两个修复都不起作用,我怀疑这是由于makefile的编写方式,也包括在下面。

这是我在编译时收到的错误输出:

BinaryNode.cpp:6:1: error: redefinition of ‘BinaryNode<ItemType>::BinaryNode()’
In file included from BinaryNode.h:35:0,
                 from BinaryNode.cpp:1:
BinaryNode.cpp:6:1: error: ‘BinaryNode<ItemType>::BinaryNode()’ previously declared here
BinaryNode.cpp:13:1: error: redefinition of ‘BinaryNode<ItemType>::BinaryNode(const     ItemType&)’
In file included from BinaryNode.h:35:0,
                 from BinaryNode.cpp:1:
BinaryNode.cpp:13:1: error: ‘BinaryNode<ItemType>::BinaryNode(const ItemType&)’         previously declared here
BinaryNode.cpp:22:1: error: redefinition of ‘BinaryNode<ItemType>::BinaryNode(const     ItemType&, BinaryNode<ItemType>*, BinaryNode<ItemType>*)’
In file included from BinaryNode.h:35:0,
                 from BinaryNode.cpp:1:
BinaryNode.cpp:22:1: error: ‘BinaryNode<ItemType>::BinaryNode(const ItemType&,     BinaryNode<ItemType>*, BinaryNode<ItemType>*)’ previously declared here
BinaryNode.cpp:30:6: error: redefinition of ‘void BinaryNode<ItemType>::setItem(const     ItemType&)’
In file included from BinaryNode.h:35:0,
                 from BinaryNode.cpp:1:

(依此类推,对于下面实现文件中定义的每个函数)

头文件(不能修改):

    #ifndef _BINARY_NODE
    #define _BINARY_NODE
template<typename ItemType>
class BinaryNode
{   
private:
   ItemType              item;           // Data portion
   BinaryNode<ItemType>* leftChildPtr;   // Pointer to left child
   BinaryNode<ItemType>* rightChildPtr;  // Pointer to right child
public:
   BinaryNode();
   BinaryNode(const ItemType& anItem);
   BinaryNode(const ItemType& anItem,
              BinaryNode<ItemType>* leftPtr,
              BinaryNode<ItemType>* rightPtr);
   void setItem(const ItemType& anItem);
   ItemType getItem() const;
   bool isLeaf() const;
   BinaryNode<ItemType>* getLeftChildPtr() const;
   BinaryNode<ItemType>* getRightChildPtr() const;
   void setLeftChildPtr(BinaryNode<ItemType>* leftPtr);
   void setRightChildPtr(BinaryNode<ItemType>* rightPtr);                
}; // end BinaryNode

这是我的实现文件(可以修改):

#include "BinaryNode.h"
#include <stddef.h>

template<class ItemType> 
BinaryNode<ItemType>::BinaryNode()
{
  leftChildPtr = NULL;
  rightChildPtr = NULL;
}
template<class ItemType> 
BinaryNode<ItemType>::BinaryNode(const ItemType& anItem) 
{
  item = anItem;
  leftChildPtr = NULL;
  rightChildPtr = NULL;
}
template<class ItemType> 
BinaryNode<ItemType>::BinaryNode(const ItemType& anItem, BinaryNode<ItemType>* leftPtr, BinaryNode<ItemType>* rightPtr)
{
  item = anItem;
  leftChildPtr = leftPtr;
  rightChildPtr = rightPtr;
}
template<class ItemType> 
void BinaryNode<ItemType>::setItem(const ItemType& anItem)
{
  item = anItem;
}
template<class ItemType> 
ItemType BinaryNode<ItemType>::getItem() const
{
  return item;
}
template<class ItemType> 
bool BinaryNode<ItemType>::isLeaf() const
{
  return ((leftChildPtr == NULL) && (rightChildPtr == NULL));    
}
template<class ItemType> 
BinaryNode<ItemType>* BinaryNode<ItemType>::getLeftChildPtr() const
{
  return leftChildPtr;
}
template<class ItemType> 
BinaryNode<ItemType>* BinaryNode<ItemType>::getRightChildPtr() const
{
  return rightChildPtr;
}
template<class ItemType> 
void BinaryNode<ItemType>::setLeftChildPtr(BinaryNode<ItemType>* leftPtr)
{
  leftChildPtr = leftPtr;
}
template<class ItemType> 
void BinaryNode<ItemType>::setRightChildPtr(BinaryNode<ItemType>* rightPtr)
{
  rightChildPtr = rightPtr;
}

还有,这是我的makefile。我有一种感觉,这就是问题所在,但经过一个小时的阅读,makefiles对我来说并不明显。

lab10: main.o BinaryNode.o
    cc -o lab10 main.o
main.o : main.cpp BinaryNode.h
    cc -c main.cpp
BinaryNode.o: BinaryNode.cpp BinaryNode.h
    cc -c BinaryNode.cpp
clean :
    rm *.o *~

我的main.cpp,它现在只在那里让编译器高兴:

#include "BinaryNode.h"
int main(int argc, char* argv[])
{

};

正如@juanchopanza所提到的,就我所知,你不能有一个单独的模板实现文件;所有的代码都需要放在头文件中。