c++重定义错误
C++ Redefinition Error
我已经寻找了这个问题的答案,并尝试了建议的解决方案。即#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所提到的,就我所知,你不能有一个单独的模板实现文件;所有的代码都需要放在头文件中。
相关文章:
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 使用命名空间时出现多个定义错误
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编写代码时C++出现错误:错误 1 错误 C2601:'circle':本地函数定义是非法的
- 在运算符重载定义中使用成员函数(const错误)
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在C++中使用内联方法时出现未定义的符号错误
- 已定义函数时出现 G++ "未定义的引用"错误
- C++ G++ 编译器 - 错误:隐式声明的定义
- 链接 cmake 时出现未定义的引用错误
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 错误:未定义对'oboe::AudioStreamBuilder::openStream(oboe::AudioStream**)'的引用
- 链接阶段出现多重定义错误
- 已经以性格错误定义了
- vim ctag导致了对乐趣的错误定义
- C++ 隐式声明的编译器错误定义
- 编译器错误-定义问题.C++