为什么我会收到这个未定义的架构符号x86_64错误
Why am I getting this undefined symbols for architecture x86_64 error
我收到一个未定义的架构符号x86_64错误,但我不确定为什么。我正在使用链表和模板制作堆栈数据类型。
StackLinkedList.h
#ifndef __StackLinkedList__StackLinkedList__
#define __StackLinkedList__StackLinkedList__
#include <iostream>
using namespace std;
#endif /* defined(__StackLinkedList__StackLinkedList__) */
template <class Item>
class StackLinkedList {
public:
StackLinkedList();
void push(Item p);
private:
StackLinkedList<Item>* node;
Item data;
};
堆栈链接列表.cpp
#include "StackLinkedList.h"
template <class Item>
StackLinkedList<Item>::StackLinkedList() {
node = NULL;
}
template <class Item>
void StackLinkedList<Item>::push(Item p) {
if(node == NULL) {
StackLinkedList<Item>* nextNode;
nextNode->data = p;
node = nextNode;
}else {
node->push(p);
}
}
主.cpp
#include "StackLinkedList.h"
int main() {
StackLinkedList<int>* stack;
stack->push(2);
}
错误详细信息:
Undefined symbols for architecture x86_64:
"StackLinkedList<int>::push(int)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我正在使用Xcode 6.1。
您必须在头文件中声明/定义模板函数,因为编译器必须在编译时使用有关实例化类型的信息。因此,将模板函数的定义放在.h
文件中,而不是放在cpp
中。
看为什么模板只能在头文件中实现?了解更多详情。
相关文章:
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 将无符号char*转换为std::istream*C++
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- vscode g++链路故障:体系结构x86_64的未定义符号
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 使用gcc从静态链接的文件中查找可选符号
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- VC++本机单元测试,找不到调试符号
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- 为x86而非x64编译时出错
- 检查TCHAR数组输入是否为带符号整数C++
- C++如何仅使用MOV在x86上实现发布和获取
- 用符号版本替换对函数的所有调用
- 在 x86-64 平台上为 C(++) 中的 64 位无符号参数计算 (a*b)%n FAST
- 为什么WinDbg无法通过Service Pack 2 x86零售符号下载从Windows Server 2003加载符