C++ 链接时出现:undefined reference to 'XXXX'

  项目开发中经常使用 makefile 编译 。也经常遇到 make 进行预编译都是正常,但是到链接时就出现 undefined reference to  'XXXX' 的异常情况。检查下来函数名和参数定义及返回参数都没有存在错误的情况下,可能一时找不到错误的地方。 这时往往忽略了,在 XXX.cpp  文件中,函数的现实缺少了含带类名。如: XXXClass::yyy( ){ } 

 

这些都是基础知识,却容易被我们所忽略。(写下时刻提醒自己)

发布了6 篇原创文章 · 获赞 7 · 访问量 4140
展开阅读全文

C++类调用函数显示:undefined reference to 'XXX'

05-29

UdpBroadcast.h文件中创建一个类及成员函数的声明,UdpBroadcast.cpp中包含成员函数的实现方法。 VADettask.cpp调用UdpBroadcast.h的函数成员显示 undefined reference to `UdpBroadcast::UdpSocketCreate() UdpBroadcast.h代码: ``` #ifndef _UDP_BROADCAST_H_ #define _UDP_BROADCAST_H_ #include <iostream> #include <stdio.h> #include <sys/socket.h> #include <unistd.h> #include <sys/types.h> #include <netdb.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> using namespace std; class UdpBroadcast { private: struct sockaddr_in sendaddr; struct sockaddr_in recvaddr; public: //UdpBroadcast(); Uint32 UdpSocketCreate(); void UdpSendProc(Int32 sockfd, const char *IP, Uint32 Port); void UdpRecvProc(Int32 sockfd, Uint32 Port); Uint32 UdpSend(Int32 sockfd, char* msg); Uint32 UdpRecv(Int32 sockfd, char *msg, Uint32 len); }; #endif ``` UdpBroadcast.cpp代码: ``` #include <iostream> #include <stdio.h> #include <sys/socket.h> #include <unistd.h> #include <sys/types.h> #include <netdb.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #include "UdpBroadcast.h" using namespace std; Uint32 UdpBroadcast::UdpSocketCreate() { Int32 sockfd; sockfd=socket(AF_INET,SOCK_DGRAM,0); if(sockfd ==-1) { cout<<"socket error"<<endl; return -1; } return sockfd; } ``` VADettask.cpp代码: ``` UdpBroadcast *m_UdpBroadcast = new(UdpBroadcast); m_UdpBroadcast->UdpSocketCreate(); ``` ![图片说明](https://img-ask.csdn.net/upload/201905/29/1559127086_413199.png) 大家知道是什么原因吗? 问答

c++ 编译报错 "undefined reference to ……"

03-17

![编译情况](https://img-ask.csdn.net/upload/201803/17/1521251236_172399.png) 总共有五个文件,Graph.h ,Graph.cpp,diamond.h.diamond.cpp,new.cpp 编译成功的是我把diamond类的声明和定义(Graph的声明和定义也放在了一个文件里,但是没有影响)都放在diamond.cpp里。 new.cpp 就是包含了"Graph.h"和"diamond.",然后用diamond创建了一个对象,调用ShapePrint()函数。 Graph.h ``` #ifndef _Graph_H_ #define _Graph_H_ class shape { public: void ShapePrint(); void ShapePrint(int x); void ShapePrint(int x,int y); int GetLength_x(); int GetLength_y(); protected: void SetLength_x(int length_x); void SetLength_y(int length_y); private: int length_x ; int length_y ; }; #endif ``` diamond.h ``` #include <iostream> #include <iomanip> #include "Graph.h" #ifndef _diamond_ #define _diamond_ class diamond:public shape{ public: diamond(); diamond(int x); int GetLength(); protected: void SetLength(int l); private: int length; }; #endif ``` diamond.cpp ``` #include "diamond.h" using namespace std; void diamond::SetLength(int l) { this->length = l; } int diamond::GetLength() { return length; } diamond::diamond() { SetLength(7); } diamond::diamond(int l) { SetLength(l); } void diamond::ShapePrint() { int length=GetLength(); for(int i=1;i<=length;++i) { cout<<setw(length-i+1)<<"*"; if(i!=1) cout<<setw(2*i-2)<<"*"<<endl; else cout<<endl; } for(int k=1;k<=length-1;k++) { cout<<setw(k+1)<<"*"; if(k==length-1) cout<<endl; else cout<<setw(2*(length-k)-2)<<"*"<<endl; } } void diamond::ShapePrint(int x) { int length=GetLength(); for(int i=1;i<=length;++i) { cout<<setw(x+(length-i+1))<<"*"; if(i!=1) cout<<setw(2*i-2)<<"*"<<endl; else cout<<endl; } for(int k=1;k<=length-1;k++) { cout<<setw(x+k+1)<<"*"; if(k==length-1) cout<<endl; else cout<<setw(2*(length-k)-2)<<"*"<<endl; } } void diamond::ShapePrint(int x,int y) { int length=GetLength(); for(int i=1;i<=y;i++) cout<<endl; for(int i=1;i<=length;++i) { cout<<setw(x+(length-i+1))<<"*"; if(i!=1) cout<<setw(2*i-2)<<"*"<<endl; else cout<<endl; } for(int k=1;k<=length-1;k++) { cout<<setw(x+k+1)<<"*"; if(k==length-1) cout<<endl; else cout<<setw(2*(length-k)-2)<<"*"<<endl; } } ``` 问答

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官方博客

分享到微信朋友圈

×

扫一扫,手机浏览