无法解析的外部命令

ZEzio 2015-05-18 07:30:16
错误 8 error LNK1120: 7 个无法解析的外部命令 E:\大学\C++\作业\The Tenth Work\Debug\The Tenth Work.exe The Tenth Work
错误 1 error LNK2019: 无法解析的外部符号 "public: __thiscall StackNode<int>::StackNode<int>(void)" (??0?$StackNode@H@@QAE@XZ),该符号在函数 _main 中被引用 E:\大学\C++\作业\The Tenth Work\The Tenth Work\The Tenth Work.obj The Tenth Work
错误 4 error LNK2019: 无法解析的外部符号 "public: bool __thiscall StackNode<int>::GetTop(int &)" (?GetTop@?$StackNode@H@@QAE_NAAH@Z),该符号在函数 _main 中被引用 E:\大学\C++\作业\The Tenth Work\The Tenth Work\The Tenth Work.obj The Tenth Work
错误 7 error LNK2019: 无法解析的外部符号 "public: bool __thiscall StackNode<int>::Pop(int &)" (?Pop@?$StackNode@H@@QAE_NAAH@Z),该符号在函数 _main 中被引用 E:\大学\C++\作业\The Tenth Work\The Tenth Work\The Tenth Work.obj The Tenth Work
错误 6 error LNK2019: 无法解析的外部符号 "public: bool __thiscall StackNode<int>::Push(int)" (?Push@?$StackNode@H@@QAE_NH@Z),该符号在函数 _main 中被引用 E:\大学\C++\作业\The Tenth Work\The Tenth Work\The Tenth Work.obj The Tenth Work
错误 3 error LNK2019: 无法解析的外部符号 "public: bool __thiscall StackNode<int>::StackEmpty(void)" (?StackEmpty@?$StackNode@H@@QAE_NXZ),该符号在函数 _main 中被引用 E:\大学\C++\作业\The Tenth Work\The Tenth Work\The Tenth Work.obj The Tenth Work
错误 2 error LNK2019: 无法解析的外部符号 "public: int __thiscall StackNode<int>::StackLength(void)" (?StackLength@?$StackNode@H@@QAEHXZ),该符号在函数 _main 中被引用 E:\大学\C++\作业\The Tenth Work\The Tenth Work\The Tenth Work.obj The Tenth Work
错误 5 error LNK2019: 无法解析的外部符号 "public: void __thiscall StackNode<int>::ClearStack(void)" (?ClearStack@?$StackNode@H@@QAEXXZ),该符号在函数 _main 中被引用 E:\大学\C++\作业\The Tenth Work\The Tenth Work\The Tenth Work.obj The Tenth Work
...全文
294 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
ForestDB 2015-05-25
  • 打赏
  • 举报
回复
有可能是16L说的原因。
yangyunzhao 2015-05-23
  • 打赏
  • 举报
回复
目前还不支持模板的分离编译
farmliver 2015-05-22
  • 打赏
  • 举报
回复
还是把类模板的定义和实现都放在一个头文件中。目前大部分编译器都不支持定义和实现分离。
youjun610789338 2015-05-19
  • 打赏
  • 举报
回复
引用 13 楼 youjun610789338 的回复:
[quote=引用 10 楼 ZEzio 的回复:] [quote=引用 9 楼 youjun610789338 的回复:] 看这出错信息,报的全是找不到STACK.cpp中方法的定义。 你项目工程结构是怎么样的?是不是用了链接库的方式编译程序,然后却没指定链接路径??
我只是写一个简单的工程,我用的只是.h引用啊,是不是类模版的使用错误了?[/quote] 类模板的使用没有错,我copy了你的代码调试了一下, 也是报同样的错误,但是把stack.cpp里的内容全部copy到后,就不报错了。或者你直接在main.cpp里#include "stack.cpp"也行, 只不过这样不规范。 至于原因,我上网找了很久,终于找到了合理的解释: 程序编译分为四个步骤:预处理->编译->汇编->链接 代码中的变量是在第二个阶段(编译)进行实例化的,而所有cpp文件是分开单独编译的,编译main.cpp的时候根据StackNode<int> *pstack = new StackNode<int>;这句代码把template <class Element>中的Element用int类型代替了,但是stack.cpp中的Element并没有被int代替,因为main.cpp和stack.cpp是分开编译的。 等到main.cpp和stack.cpp都编译好后就会进行最后的链接,但是stack.cpp中Element并没有被int类型代替,所以就会找不到StackNode<int>::StackNode<int>(void)这个函数的定义。 [/quote] 所以模板的定义和声明都要放到同一个文件里面!
youjun610789338 2015-05-19
  • 打赏
  • 举报
回复
引用 10 楼 ZEzio 的回复:
[quote=引用 9 楼 youjun610789338 的回复:] 看这出错信息,报的全是找不到STACK.cpp中方法的定义。 你项目工程结构是怎么样的?是不是用了链接库的方式编译程序,然后却没指定链接路径??
我只是写一个简单的工程,我用的只是.h引用啊,是不是类模版的使用错误了?[/quote] 类模板的使用没有错,我copy了你的代码调试了一下, 也是报同样的错误,但是把stack.cpp里的内容全部copy到后,就不报错了。或者你直接在main.cpp里#include "stack.cpp"也行, 只不过这样不规范。 至于原因,我上网找了很久,终于找到了合理的解释: 程序编译分为四个步骤:预处理->编译->汇编->链接 代码中的变量是在第二个阶段(编译)进行实例化的,而所有cpp文件是分开单独编译的,编译main.cpp的时候根据StackNode<int> *pstack = new StackNode<int>;这句代码把template <class Element>中的Element用int类型代替了,但是stack.cpp中的Element并没有被int代替,因为main.cpp和stack.cpp是分开编译的。 等到main.cpp和stack.cpp都编译好后就会进行最后的链接,但是stack.cpp中Element并没有被int类型代替,所以就会找不到StackNode<int>::StackNode<int>(void)这个函数的定义。
Minikinfish 2015-05-19
  • 打赏
  • 举报
回复
引用 2 楼 ID870177103 的回复:
模板类的实现也要放在.h里
按照这个来处理吧
fly_dragon_fly 2015-05-19
  • 打赏
  • 举报
回复
这是链接错误 public: __thiscall StackNode<int>::StackNode<int>(void),这是你的第一条,提示在main函数中引用了这个函数,这显然是构造函数,这个函数的{}之间的内容在那个文件?
ZEzio 2015-05-18
  • 打赏
  • 举报
回复
引用 9 楼 youjun610789338 的回复:
看这出错信息,报的全是找不到STACK.cpp中方法的定义。 你项目工程结构是怎么样的?是不是用了链接库的方式编译程序,然后却没指定链接路径??
我只是写一个简单的工程,我用的只是.h引用啊,是不是类模版的使用错误了?
youjun610789338 2015-05-18
  • 打赏
  • 举报
回复
看这出错信息,报的全是找不到STACK.cpp中方法的定义。 你项目工程结构是怎么样的?是不是用了链接库的方式编译程序,然后却没指定链接路径??
ZEzio 2015-05-18
  • 打赏
  • 举报
回复
#include "stdafx.h" #include "Stack.h" #include <cstdlib> #include <iostream> using namespace std; /* * 创建空栈 */ template <class Element> StackNode<Element>::StackNode() { size = STACK_INIT_SIZE; space = (Element*)malloc(size * sizeof(Element)); if (space == NULL) exit(-1); top = space; } /* * 返回 S 的元素个数,即栈的长度 */ template <class Element> int StackNode<Element>::StackLength() { return top - space; } /* * 若栈 S 为空栈,则返回TRUE,否则 FALSE */ template <class Element> bool StackNode<Element>::StackEmpty() { return top == space; } /* * 取栈顶元素 */ template <class Element> bool StackNode<Element>::GetTop(Element &p) { if (top == space) { printf("No element in the stack!\n"); return false; } p = *(top - 1); return true; } /* * 将 S 清为空栈 */ template <class Element> void StackNode<Element>::ClearStack() { memset(space, 0, sizeof(space)); top = space; return; } /* * 插入元素 p 为新的栈顶元素 */ template <class Element> bool StackNode<Element>::Push(Element p) { if (top - space >= size) { Element *tmp = (Element*)realloc(space, (STACK_INIT_SIZE + STACKINCREMENT) * sizeof(StackNode)); if (tmp == NULL) { printf("No enough space for new elem!\n"); return false; } space = tmp; top = space + size; size += STACKINCREMENT; } *top = p; ++top; return true; } /* * 删除 S 的栈顶元素,并用 p 返回其值 */ template <class Element> bool StackNode<Element>::Pop(Element &p) { if (space == top) { printf("No elem in the stack!\n"); return false; } *top = 0; --top; return true; } /* * 释放栈 */ template <class Element> StackNode<Element>::~StackNode() { free(space); space = top = NULL; size = 0; } 这是函数的实现
ZEzio 2015-05-18
  • 打赏
  • 举报
回复
#ifndef STACK #define STACK // #include "stdafx.h" #include <cstdio> #include "stdlib.h" #include <iostream> // #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 // template <class Element> class StackNode { private: Element *top, *space; int size; public: StackNode(); // 返回 S 的元素个数,即栈的长度 int StackLength(); // 若栈 S 为空栈,则返回TRUE,否则 FALSE bool StackEmpty(); // 取栈顶元素 bool GetTop(Element &p); // 将 S 清为空栈 void ClearStack(); // 插入元素 e 为新的栈顶元素 bool Push(Element p); // 删除 S 的栈顶元素,并用 e 返回其值 bool Pop(Element &p); // ~StackNode(); }; // #endif 这是定义的.h
sprawling 2015-05-18
  • 打赏
  • 举报
回复
信息太少了,主要要看实现文件。
苏叔叔 2015-05-18
  • 打赏
  • 举报
回复
仅从测试用例看不出问题
ZEzio 2015-05-18
  • 打赏
  • 举报
回复
#include "stdafx.h" #include "stack.h" #include "stdlib.h" #include <iostream> #include <cstdio> #pragma comment(lib,"winmm.lib") using namespace std; int main() { StackNode<int> *pstack = new StackNode<int>; int s = 3; cout << pstack->StackEmpty() << endl; cout << pstack->StackLength() << endl; cout << pstack->GetTop(s) << endl; cout << pstack->Pop(s) << endl; pstack->ClearStack(); cout << pstack->Push(s) << endl; cout << pstack->GetTop(s) << endl; cout << s << endl; cout << pstack->Pop(s) << endl; cout << s << endl; pstack->ClearStack(); return 0; } 这是测试用的源码,用的是VS2013的环境
ZEzio 2015-05-18
  • 打赏
  • 举报
回复
是说模版类不能另外开一个CPP来写实现吗?
ID870177103 2015-05-18
  • 打赏
  • 举报
回复
模板类的实现也要放在.h里
ZEzio 2015-05-18
  • 打赏
  • 举报
回复
如题,写作业遇到这个问题,用的是类模版来实现栈。然后就报这样的错误。谢谢各位大大

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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