BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #71649同步于 2013/6/7
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

Re: [问题]error LNK2001的问题,求解惑!

ximenchuixie
2013/6/7镜像同步13 回复
那几个方法只有声明没有定义
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
lcl13hope机器人#1 · 2013/6/8
【 在 ximenchuixie 的大作中提到: 】 : 那几个方法只有声明没有定义 你好,应该是有的!结构是这样,我在LinearList.h 定义了线性表的模板类,然后定义了seqlist.h来实现linearlist.h中模板类的虚函数和纯虚函数,seqlistu.h中定义了两个线性表求并的方法
q397273499机器人#2 · 2013/6/8
LinearList::Insert LinearList::Delete LinearList::Update 确定都实现了?
q397273499机器人#3 · 2013/6/8
如果是模板类的话,试试写在同一个文件里? 【 在 lcl13hope 的大作中提到: 】 : 你好,应该是有的!结构是这样,我在LinearList.h 定义了线性表的模板类,然后定义了seqlist.h来实现linearlist.h中模板类的虚函数和纯虚函数,seqlistu.h中定义了两个线性表求并的方法
zdybupt机器人#4 · 2013/6/8
模板类 的 函数 需要和调用它的程序写在一个文件里,实例化的时候才会生成代码,如果不在一个文件里,独立编译完了以后没有相应实例的代码,肯定会出链接错误
lcl13hope机器人#5 · 2013/6/8
【 在 q397273499 的大作中提到: 】 : LinearList::Insert : LinearList::Delete : LinearList::Update : ................... 在seqlist.h中的定义了class SeqList: public LinearList<T>, class SeqList: public LinearList<T> { public: SeqList(int mSize); ~SeqList(){delete [] elements;} bool IsEmpty() const; int Length() const; bool Find(int i, T & x) const; int Search(T x) const; bool Insert(int i,T x); bool Delete(int i); bool Update(int i,T x); void Output(ostream &out) const; private: int maxLength; T *elements; }; 之后对各个方法进行了实现
lcl13hope机器人#6 · 2013/6/8
【 在 zdybupt 的大作中提到: 】 : 模板类 的 函数 需要和调用它的程序写在一个文件里,实例化的时候才会生成代码,如果不在一个文件里,独立编译完了以后没有相应实例的代码,肯定会出链接错误 你好,我定义了三个头文件,然后main中引用seqlistu.h(我理解只引用这一个头文件就可以了,因为seqlistu.h中引用了其他头文件),在VS2012下实现的代码,这样用不对么?麻烦再帮忙看一下.. linearlist.h中包含 template <class T> class LinearList { public : virtual bool IsEmpty() const = 0; virtual int Length() const = 0; virtual bool Find(int i, T & x) const = 0; virtual int Search(T x) const = 0; virtual bool Insert(int i,T x); virtual bool Delete(int i); virtual bool Update(int i,T x); virtual void Output(ostream &out) const = 0; protected: int n; }; SeqList.h中包括如下,并且有对SeqList类中的方法进行了实现。 #include "linearlist.h" template <class T> class SeqList: public LinearList<T> { public: SeqList(int mSize); ~SeqList(){delete [] elements;} bool IsEmpty() const; int Length() const; bool Find(int i, T & x) const; int Search(T x) const; bool Insert(int i,T x); bool Delete(int i); bool Update(int i,T x); void Output(ostream &out) const; private: int maxLength; T *elements; }; seqlistu.h包括: #include "seqlist.h" template <class T> void Union(SeqList<T> &LA,SeqList<T> &LB) { T x; for(int i = 0;i<LB.Length();i++) { LB.Find(i,x); if (LA.Search(x) == -1) { LA.Insert(LA.Length()-1,x); } } }
iliketour机器人#7 · 2013/6/8
就是3楼说的 编译过了,连链接错误就是方法没有实现
zhangywlfh机器人#8 · 2013/6/8
template <class T> class LinearList 中 virtual bool Insert(int i,T x); virtual bool Delete(int i); virtual bool Update(int i,T x); lz 原因在这个地方哦 木有定义为纯虚函数(编译器当做是类的接口,可以不实现) 如果不定义为纯虚函数 则就需要给出函数实体,不然肯定有连接错误 无法解析的外部符号 建议楼主查考虚函数的实现机制:编译器会帮类安插一个指针 vptr,vptr指向virtual function table, 这个table中存放的是虚函数的地址,如果虚函数木有实现,地址到哪儿去取呢? 你上面的连接错误应该出现在这个地方吧 而纯虚函数的话,table中仅仅为它留了一个位置,等到派生类中再去派生类该函数的地址即可 加三个 “=0” 就解决鸟 bless!
q397273499机器人#9 · 2013/6/8
9l正解 lz的代码链接报错指出了下面3个没有定义 LinearList::Insert LinearList::Delete LinearList::Update 正像9l说的,这三个声明时非纯虚函数,想必lz以为在seqlist里面实现了就行了,而没有写linearlist的对应实现,结果链接时找不到符号 【 在 zhangywlfh 的大作中提到: 】 : template <class T> class LinearList 中 : virtual bool Insert(int i,T x); : virtual bool Delete(int i); : ...................