求解C++模板与LNK2005 ***obj 已经定义的问题
我定义了一个模板类A,然后在另一个类B中实例化那个A,编译没有问题,但是运行的时候就出现了问题,我贴出部分代码吧。。。
Chain 模板类
template<class DataType>
class Chain
{
private:
ChainNode<DataType> *first;
public:
Chain(void);
Chain(Chain<DataType> ©);
bool IsEmpty() const {return first==0;}
int Length() const;
bool Find(int k,DataType& x) const;
int Search(const DataType& x) const;
Chain<DataType>& Delete(int k,DataType& x);
Chain<DataType>& Insert(int k,const DataType x);
void Sort() ;
void Clear() ;
void Output(ostream& out) const;
void Combination(const Chain& a,const Chain<DataType>& b);
//friend ostream& operator << (ostream& out,Chain<DataType>& x); //采用函数的方式调用可以不用友元
public:
~Chain(void);
};
Interface类
#pragma once
#include<iostream>
#include"Chain.cpp"
using namespace std;
class Interface
{
public:
Interface(void);
void List_Manipulation(Chain<int> &List);
void Search(const Chain<int> &List) const;
void GetLength(const Chain<int> &List) const;
void Combine(const Chain<int> &List) const;
void Output(const Chain<int> &List) const;
void Operation();
public:
~Interface(void);
};
在头文件那里,我在Interface类里面是调入#include"Chain.cpp"的,
在Interface的一些函数里面是有定义一个Chain<int>的类,但是不知道
为什么在编译的时候没有问题,却在运行的时候会出现下面的一些错误:
错误 1 error LNK2005: "public: __thiscall Interface::Interface(void)" (??0Interface@@QAE@XZ) 已经在 Interface.obj 中定义 LinkStruct.obj
错误 2 error LNK2005: "public: __thiscall Interface::~Interface(void)" (??1Interface@@QAE@XZ) 已经在 Interface.obj 中定义 LinkStruct.obj
错误 3 error LNK2005: "public: void __thiscall Interface::GetLength(class Chain<int> const &)const " (?GetLength@Interface@@QBEXABV?$Chain@H@@@Z) 已经在 Interface.obj 中定义 LinkStruct.obj
错误 4 error LNK2005: "public: void __thiscall Interface::Output(class Chain<int> const &)const " (?Output@Interface@@QBEXABV?$Chain@H@@@Z) 已经在 Interface.obj 中定义 LinkStruct.obj
错误 5 error LNK2005: "public: void __thiscall Interface::Search(class Chain<int> const &)const " (?Search@Interface@@QBEXABV?$Chain@H@@@Z) 已经在 Interface.obj 中定义 LinkStruct.obj
错误 6 error LNK2005: "public: void __thiscall Interface::Operation(void)" (?Operation@Interface@@QAEXXZ) 已经在 Interface.obj 中定义 LinkStruct.obj
错误 7 fatal error LNK1169: 找到一个或多个多重定义的符号 E:\MyProjects\List_Linked\Debug\List_Linked.exe
这个问题已经困扰了好多天,上网也查不到相关的资料。。。
望各位大虾指点一下。。。