C++链接器错误 - 看不到为什么它没有链接?(包括完整的代码示例)

C++ linker error - cannot see why its not linking? (full complete code example included)

本文关键字:链接 包括完 代码 错误 看不到 为什么 C++      更新时间:2023-10-16

这是我的代码,我在构造函数上遇到了链接问题:

#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

我不明白为什么这些没有链接?

模板类定义及其成员函数实现应位于一个头文件中。