DEV C++ linker error问题 多文件联合编译
stack.hpp文件
/*--------------------------------------------------------------
Header file--This file contain declarations for class stack.
(c++) Written by:
Date:2012-10-09
--------------------------------------------------------------*/
#ifndef STACK_HPP
#define STACK_HPP
#include <iostream>
using namespace std ;
class Stack
{
public :
enum { MaxSize = 6 };
Stack() { top = -1 ; }
//void push(int& element) ;
//void pop() ;
//void dump() ;
//inline bool isFull() ;
inline bool isEmpty();
private:
//void errMsg(const char* pMsg) const ;
int stack[MaxSize] ;
int top ;
int any_val ;
};
#endif
stack.cpp文件
*--------------------------------------------------------------
stack.cpp--Define methods for class stack.
(c++) Written by:
Date:2012-10-09
--------------------------------------------------------------*/
#include "stack.hpp"
/*=======================isEmpty===============================
*/
bool Stack::isEmpty()
{
return top < 0 ;
}//stack::isEmpty
client.cpp文件
/*--------------------------------------------------------------
client.cpp--Test Driver
(c++) Written by:
Date:2012-10-09
--------------------------------------------------------------*/
#include "stack.hpp"
int main()
{
//Local declarations
Stack st ;
//Statements
if(st.isEmpty())
{
cout << "Ok" ;
}
system("pause") ;
return 0 ;
}//main
我已经建立了工程
错误信息:
[Linker error] undefined reference to `Stack::isEmpty()'
ld returned 1 exit status
H:MyCodedownloadCPPstudyProcessExampleschapter03CLASSstackMakefile.win [Build Error] [STACK.exe] Error 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
模板的分离编译,一般情况都是不把模版类的.h和.cpp分离,万不得已,那么就在需要使用自定义模版的地方使用#include "xxx.cpp"文件,这样可以编译连接成功
十分感谢你的回答,的确是这样
Note: It's imperative that the function's definition (the part between the {...}) be placed in a header file, unless the function is used only in a single .cpp file. In particular, if you put the inline function's definition into a .cpp file and you call it from some other .cpp file, you'll get an "unresolved external" error from the linker.
把inline移除或者把函数定义放到.hpp里面