还是不能解决dev=c++下类模板编译[linker error]问题

huangweibuct 2009-02-27 11:05:24
#include <iostream> 
using namespace std;

const int MAX=5;

template <class T>
class Z
{
private:
int top;
T s[MAX];
public:
Z(){top=-1;}
void push(const T& input);
T pop();
int emptystack()const;
};

template <class T>
void Z<T>::push(const T& input)
{
if(top==MAX-1)
{
cout <<"栈满溢出" <<endl;
exit(1);
}
top++;
s[top]=input;
}

template <class T>
T Z <T>::pop()
{
T temp;
if(top==-1)
{
cout <<"栈为空" <<endl;
exit(1);
}
temp=s[top];
top--;
return temp;
}

template <class T>
int Z <T>::emptystack()const
{
return top==-1;
}

int main()
{
Z<int> st1;
int a[]={4,8,3,2};
for(int i=0;i<4;i++)
{
st1.push(a[i]);
}
while(!st1.emptystack())
cout <<st1.pop();
system("pause");
return 0;
}

这样放在一个文件里可以了。
但是分开放还是提示上面那个无法连接。我再将三个文件发一遍。。崩溃了。
Z.h: 
#ifndef Z_H
#define Z_H
#include <iostream>
using namespace std;
const int MAX=5;
template <class T>
class Z
{
private:
int top;
T s[MAX];
public:
Z(){top=-1;}
void push(const T& input);
T pop();
int emptystack()const;
};
#endif
-----------------------------------------------
z.cpp:
#include"Z.h"
#include <iostream>
using namespace std;
template <class T>
void Z <T>::push(const T& input)
{
if(top==MAX-1)
{
cout < <"栈满溢出" < <endl;
exit(1);
}
top++;
s[top]=input;
}
template <class T>
T Z <T>::pop()
{
T temp;
if(top==-1)
{
cout < <"栈为空" < <endl;
exit(1);

}
temp=s[top];
return temp;
}
template <class T>
int Z <T>::emptystack()const
{
return top==-1;
}
-------------------------------------
zmain.cpp:
#include"Z.h"
#include <iostream>
using namespace std;
int main()
{
Z <int> st1;
int a[]={4,8,3,2};
for(int i=0;i <4;i++)
{
st1.push(a[i]);
}
while(!st1.emptystack())
cout < <st1.pop();
system("pause");
return 0;
}
-----------------------------
编译错误:
[Linker error] undefined reference to `Z <int>::push(int const&)'
[Linker error] undefined reference to `Z <int>::emptystack() const'
[Linker error] undefined reference to `Z <int>::pop()'
ld returned 1 exit status
D:\Program Files\DEV-CPP\Makefile.win [Build Error] [工程1.exe] Error 1


小辈初学c++,请高手指教,谢谢啦
...全文
208 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fallening 2009-02-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hairetz 的回复:]
别讨论这个囧问题了。。
全世界唯一实现了export特性的是EDG,他们反映说,就他们的经验来看,export是至今最难实现的C++特性,与他们实现过的其它三大主要特性(例如名字空间或成员模板)耗费的工作量不相上下。仅是export本身就耗费了不止三个人年来编码和测试,而且这还没算上设计工作的开销,而同样这三个人实现整个java语言也只不过耗费了两个人年。
http://celerycabbage.spaces.live.com/blog/cns!248937D2948589E0!419…
[/Quote]
此言差矣,我现在用的intel C++ compiler v11 也已经支持export了,而且是很久之前的事情了
waizqfor 2009-02-27
  • 打赏
  • 举报
回复
[Quote=引用楼主 huangweibuct 的帖子:]
C/C++ code#include <iostream>
using namespace std;

const int MAX=5;

template <class T>
class Z
{
private:
int top;
T s[MAX];
public:
Z(){top=-1;}
void push(const T& input);
T pop();
int emptystack()const;
};

template <class T>
void Z<T>::push(const T& input)
{
if(top==MAX-1)
{
cout <<"栈满溢出" <<endl;

[/Quote]
LZ是又是被套进去了 编译器不是万能的 每个编译器支持标准都不同
准备几个编译器是很有必要的
  • 打赏
  • 举报
回复
别讨论这个囧问题了。。
全世界唯一实现了export特性的是EDG,他们反映说,就他们的经验来看,export是至今最难实现的C++特性,与他们实现过的其它三大主要特性(例如名字空间或成员模板)耗费的工作量不相上下。仅是export本身就耗费了不止三个人年来编码和测试,而且这还没算上设计工作的开销,而同样这三个人实现整个java语言也只不过耗费了两个人年。
http://celerycabbage.spaces.live.com/blog/cns!248937D2948589E0!419.entry
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20080910/10/5546c282-34a0-4126-94db-8ed553c4dfb2.html
这个帖子也讨论过。
  • 打赏
  • 举报
回复
可能是因为VC等编译器不支持export吧,模板还是别分开声明和定义。别去折磨写编译器的人了,号称为了支持export关键字,花费了一个编译器3人年的时间。
有模板就把定义跟声明都放头文件了就可以了。
c语言编译程序 Version 4.9.9.1 * Many bug fixes * Improved editor Version 4.9.9.0 * Support for latest Mingw compiler system builds * Bug fixes Version 4.9.8.9 * New code tooltip display * Improved Indent/Unindent and Remove Comment * Improved automatic indent * Added support for the "interface" keyword * WebUpdate should now report installation problems from PackMan * New splash screen and association icons * Improved installer * Many bug fixes Version 4.9.8.7 * Added support for GCC > 3.2 * Debug variables are now resent during next debug session * Watched Variables not in correct context are now kept and updated when it is needed * Added new compiler/linker options: - Strip executable - Generate instructions for a specific machine (i386, i486, i586, i686, pentium, pentium-mmx, pentiumpro, pentium2, pentium3, pentium4, k6, k6-2, k6-3, athlon, athlon-tbird, athlon-4, athlon-xp, athlon-mp, winchip-c6, winchip2, k8, c3 and c3-2) - Enable use of processor specific built-in functions (mmmx, sse, sse2, pni, 3dnow) * "Default" button in Compiler Options is back * Error messages parsing improved * Bug fixes Version 4.9.8.5 * Added the possibility to modify the value of a variable during debugging (right click on a watch variable and select "Modify value") * During Dev-C++ First Time COnfiguration window, users can now choose between using or not class browser and code completion features. * Many bug fixes Version 4.9.8.4 * Added the possibility to specify an include directory for the code completion cache to be created at Dev-C++ first startup * Improved code completion cache * WebUpdate will now backup downloaded DevPaks in Dev-C++\Packages directory, and Dev-C++ executable in devcpp.exe.BACKUP * Big speed up in function parameters listing while editing * Bug fixes Version 4.9.8.3 * On Dev-C++ first time configuration dialog, a code completion cache of all the standard include files can now be generated. * Improved WebUpdate module * Many bug fixes Version

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧