读书人

为啥用标准模板库的stacklt;intgt;amp;作形参

发布时间: 2012-11-13 10:00:50 作者: rapoo

为什么用标准模板库的stack<int>&作形参会显示没定义
t.h文件

C/C++ code
   #ifndef T_H   #define T_H   #include<stack>   using namespace std;   void out(stack<int>&);   #endif                     

t.cpp文件
C/C++ code
 #include<stack> using namespace std; void out(stack<int>&a)  {      a.push(1);       a.push(2);       while(!a.empty())       {           cout<<a.top()<<endl;           a.pop();       }      }  


main.cpp文件
C/C++ code
 #include<iostream>  using namespace std;  #include "t.h"  int main()  {       stack<int>a;       out(a);       return 0;   }  


显示这个错误

main.cpp:(.text+0x3e): undefined reference to `out(std::stack<int, std::deque<int, std::allocator<int> > >&)'
collect2: ld returned 1 exit status
这是为什么啊?

[解决办法]
t.cpp改成这样即可:
C/C++ code
#include "t.h"#include<iostream>using namespace std;void out(stack<int>&a)  {      a.push(1);       a.push(2);       while(!a.empty())       {           cout<<a.top()<<endl;           a.pop();       }      } 

读书人网 >C++

热点推荐