C++链接器错误 - 看不到为什么它没有链接?(包括完整的代码示例)
C++ linker error - cannot see why its not linking? (full complete code example included)
这是我的代码,我在构造函数上遇到了链接问题:
#ifndef LOCKFREEQUEUE_H
#define LOCKFREEQUEUE_H
#include <atomic>
#include <memory>
template<typename T>
class lock_free_queue
{
private:
struct node
{
std::shared_ptr<T> data;
node* next;
node():next(nullptr){}
};
std::atomic<node*> head;
std::atomic<node*> tail;
node* pop_head()
{
node* const old_head=head.load();
if(old_head==tail.load())
{
return nullptr;
}
head.store(old_head->next);
return old_head;
}
lock_free_queue(const lock_free_queue& other);
lock_free_queue& operator=(const lock_free_queue& other);
public:
lock_free_queue();
~lock_free_queue();
std::shared_ptr<T> pop();
void push(T new_value);
};
源文件:
#include "lock_free_queue.h"
template<typename T>
lock_free_queue<T>::lock_free_queue(const lock_free_queue& other)
{
}
template<typename T>
lock_free_queue<T>& lock_free_queue<T>::operator=(const lock_free_queue& other)
{
}
template<typename T>
lock_free_queue<T>::lock_free_queue():head(new node),tail(head.load())
{
}
template<typename T>
lock_free_queue<T>::~lock_free_queue()
{
while(node* const old_head=head.load())
{
head.store(old_head->next);
delete old_head;
}
}
template<typename T>
std::shared_ptr<T> lock_free_queue<T>::pop()
{
node* old_head=pop_head();
if(!old_head)
{
return std::shared_ptr<T>();
}
std::shared_ptr<T> const res(old_head->data);
delete old_head;
return res;
}
template<typename T>
void lock_free_queue<T>::push(T new_value)
{
std::shared_ptr<T> new_data(std::make_shared<T>(new_value));
node* p=new node;
node* const old_tail=tail.load();
old_tail->data.swap(new_data);
old_tail->next=p;
tail.store(p);
}
主要:
#include "lock_free_queue.h"
int main(){
lock_free_queue<int>* my_queue = new lock_free_queue<int>();
my_queue->push(3);
return 1;
}
链接器错误:
Main.obj : error LNK2019: unresolved external symbol "public: __cdecl lock_free_queue<int>::lock_free_queue<int>(void)" (??0?$lock_free_queue@H@@QEAA@XZ) referenced in function main
Main.obj : error LNK2019: unresolved external symbol "public: void __cdecl lock_free_queue<int>::push(int)" (?push@?$lock_free_queue@H@@QEAAXH@Z) referenced in function main
我不明白为什么这些没有链接?
模板类定义及其成员函数实现应位于一个头文件中。
相关文章:
- 如何在 android ndk 上链接 C 和 C++ 代码,以及 C 和 C++ 运行时库(包括 STL)?
- 链接错误,包括我创建的相同头文件 - C++
- 在单元测试项目中包括 .c 文件,并从多个 cpp 文件访问它而不会出现链接问题
- 强制链接器失败并出现多重定义错误,即使包括 --whole-archive
- 用libclang解析源文件 - 链接问题包括文件
- Xcode(版本9.2(9C40B)):试图包括Boost时C 语义和链接器问题
- 如何包括使用软链接的工作
- VC 链接器错误使用Dllimport/DLLEXPORT宏将标题包括在多个项目中
- 将框架包括在Xcode C 项目中 - 链接器错误
- 在另一个项目中包括继承的类时,抽象类的链接器错误
- VS 2008:链接命令包括我没有要求且不存在的库
- C++链接器错误 - 看不到为什么它没有链接?(包括完整的代码示例)
- 链接时不包括宏定义
- 博兰德C++链接器错误c文件包括
- 链接器不包括未引用的数组
- 在 Visual Studio 2015 的 dll 项目中包括自己的资源文件时出现链接器错误Microsoft
- 在链表节点中递归打印数据,但使用多个前向链接C++(包括Pic)
- 链接器错误:对“vtable for square”的未定义引用.代码包括虚拟函数
- 使用GCC的链接器错误,包括binutils和textinfo
- 只包括头文件的链接