undefined reference to 错误的解决方法总结

在编写C++时,会遇到一类错误的错。

总结起来为几类

一、是 Linux环境错误  具体没有深入研究,可以参考http://blog.csdn.net/cserchen/article/details/5503556。

二、使用codeblock这类需要自己配置路径,要把所有文件添加到Build target中。配置方法参考下面两个博客:

      http://blog.csdn.net/littlealamo/article/details/9256645

      http://blog.csdn.net/subkiller/article/details/8349072

三、使用类模板时,C++中,类的声明和类方法定义不能分开放在单独文件中,有些C++提供export关键字。但新的c++没有此项功能了。如果不注意就会出现

     C:\cdblock\queue\main.cpp|18|undefined reference to `LinkQueue<dancer>::LinkQueue()'|这样错误。

发布了4 篇原创文章 · 获赞 3 · 访问量 3650
展开阅读全文

C++ 编译错误: Error:undefined reference to ` '

05-08

我编写了一个从list母类继承的链表linkList子类并把声明写在linkList.h文件中,实现写在linkList.cpp中, 但是我发现只用#include "linkList.h"的话会报错如下![图片说明](https://img-ask.csdn.net/upload/201805/08/1525751289_288763.png) 但是如果加上#include "linkList.cpp" 则不会报错。 希望大神告知错误所在,以及教我一下如何正确的书写头文件和源文件! 以下是代码: linkList.h: #ifndef LINKLIST_H_INCLUDED #define LINKLIST_H_INCLUDED class OutOfBound {}; class IllegalSize {}; template <class T> class list { public: virtual int length() const = 0; virtual void clear() = 0; virtual void insert(int i,const T& x) = 0; virtual void remove(int i) = 0; virtual T visit(int i) const = 0; virtual int search(const T& x) const = 0; virtual void traverse() const = 0; virtual ~list() {}; }; template <class T> class linkList:public list<T> { private: struct node { T data; node *prev, *next; node(const T &x, node* p = NULL, node* n = NULL) { data = x; prev = p; next = n; } node():next(NULL), prev(NULL) {} ~node() {} }; node *head, *tail; int currentLength; node* move(int i) const; public: linkList(); ~linkList() { clear(); delete head; delete tail; } int length() const { return currentLength; } void clear(); void insert(int i,const T& x); void remove(int i); T visit(int i) const; int search(const T& x) const; void traverse() const; }; #endif // LINKLIST_H_INCLUDED linkList.cpp: //file: #include <iostream> #include <cstdio> #include "linkList.h" using namespace std; template <class T> typename linkList<T>::node* linkList<T>::move(int i) const { node* p = head -> next; if(i < 0 || i > currentLength) throw OutOfBound(); while(i > 0) { p = p -> next; i--; } return p; } template <class T> linkList<T>::linkList() { head = new node; tail = new node; head -> next = tail; tail -> prev = head; currentLength = 0; } template <class T> void linkList<T>::clear() { node *p, *q; p = head -> next; while(p != tail) { q = p -> next; delete p; p = q; } head -> next = tail; tail -> prev = head; currentLength = 0; } template <class T> void linkList<T>::insert(int i, const T& x) { node *pos = move(i); node *tmp = new node(x, pos -> prev, pos); pos -> prev -> next = tmp; pos -> prev = tmp; ++currentLength; } template <class T> void linkList<T>::remove(int i) { node *pos = move(i); pos -> prev -> next = pos -> next; pos -> next -> prev = pos -> prev; delete pos; --currentLength; } template <class T> int linkList<T>::search(const T& x) const { int i = 0; node* p = head -> next; while(p != tail && p -> data != x) { p = p -> next; i++; } if( p == tail) return -1; else return i; } template <class T> T linkList<T>::visit(int i) const { node* p = move(i); return p -> data; } template <class T> void linkList<T>::traverse() const { node *p = head -> next; while(p != tail) { cout << p -> data << " "; p = p -> next; } cout << endl; } main.cpp: #include <iostream> #include "linkList.h" using namespace std; int main() { linkList<int> l1; int i; char ch; for(i = 0; i < 100; i++) { l1.insert(i, i); } l1.traverse(); ch = cin.get(); for(i = 50; i > 0; i--) { l1.remove(i); } l1.traverse(); ch = cin.get(); cout << l1.length() << endl; ch = cin.get(); for(i = 0; i < l1.length() ; ++i) { cout << l1.visit(i) << endl; } l1.traverse(); ch = cin.get(); for(i = 60; i < 80 ; ++i) { cout << l1.search(i) << endl; } l1.traverse(); ch = cin.get(); return 0; } 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览