获取LNK2019、LNK2001 LNK1120编译和运行调度程序。使用队列模板、节点模板和 cpp 文件 [重复]

Getting LNK2019, LNK2001, LNK1120 Compiling and Running a Scheduler Program. Using a queue template, node template, and to cpp files [duplicate]

提问人:Jay Harris 提问时间:7/25/2021 最后编辑:Alan BirtlesJay Harris 更新时间:7/25/2021 访问量:30

问:

我正在开发一个 shceduler 程序,该程序旨在接收多个进程,为它们分配 id、优先级编号和突发时间,然后将它们添加到队列中。调度程序将调用这些进程,打印出它们的信息(即 ID、系统中的时间等),然后将它们从队列中除名/弹出)

当我尝试编译并运行它时,我收到几个链接器错误,我将其附加在代码的底部。我有一个节点头、节点实现、队列头、队列实现、调度程序头和实现(包含我的主函数)、进程头和实现。进程文件为每个进程创建信息(即 ID、优先级编号、突发时间等),shceduler 函数将它们推送/登记到队列中,将它们从队列中调用,打印出它们的信息,并将它们弹出/从队列中除名。

如前所述,main 函数位于头文件中

节点头

#ifndef MAIN_SAVITCH_NODE2_H
#define MAIN_SAVITCH_NODE2_H
#include <cstdlib> 
#include <iterator> 
namespace main_savitch_8C
{
    template <class Item>
    class node
    {
    public:
        typedef Item value_type;
        node(const Item& init_data = Item(), node* init_link = NULL)
        {
            data_field = init_data; link_field = init_link;
        }
        Item& data() { return data_field; }
        node* link() { return link_field; }
        void set_data(const Item& new_data)
        {
            data_field = new_data;
        }
        void set_link(node* new_link) { link_field = new_link; }
        const Item& data()
            const {
            return data_field;
        }
        const node* link()
            const {
            return link_field;
        }
    private:
        Item data_field;
        node* link_field;
    };
    template <class Item>
    void list_clear(node<Item>*& head_ptr);
    template <class NodePtr, class Item>
    NodePtr list_search(NodePtr head_ptr, const Item& target);
    template <class Item>
    void list_copy(const node<Item>* source_ptr, node<Item>*& head_ptr, node<Item>*& tail_ptr);
    template <class Item>
    void list_head_insert(node<Item>*& head_ptr, const Item& entry);
    template <class Item>
    void list_head_remove(node<Item>*& head_ptr);
    template <class Item>
    void list_insert(node<Item>* previous_ptr, const Item& entry);
    template <class Item>
    std::size_t list_length(const node<Item>* head_ptr);
    template <class NodePtr, class SizeType>
    NodePtr list_locate(NodePtr head_ptr, SizeType position);
    template <class Item>
    void list_remove(node<Item>* previous_ptr);
    template <class NodePtr, class Item>
    NodePtr list_search(NodePtr head_ptr, const Item& target);
}
#include "node2.template"
#endif

节点模板

#include <cassert>    // Provides assert
#include <cstdlib>    // Provides NULL and size_t

namespace main_savitch_8C
{
    template <class Item>
    void list_clear(node<Item>*& head_ptr)
    // Library facilities used: cstdlib
    {
    while (head_ptr != NULL)
        list_head_remove(head_ptr);
    }

    template <class Item>
    void list_copy(
    const node<Item>* source_ptr,
    node<Item>*& head_ptr,
    node<Item>*& tail_ptr
    ) 
    // Library facilities used: cstdlib
    {
    head_ptr = NULL;
    tail_ptr = NULL;

    // Handle the case of the empty list
    if (source_ptr == NULL)
        return; 

    // Make the head node for the newly created list, and put data in it
    list_head_insert(head_ptr, source_ptr->data( ));
    tail_ptr = head_ptr; 
    
    // Copy rest of the nodes one at a time, adding at the tail of new list
    source_ptr = source_ptr->link( ); 
        while (source_ptr != NULL)
    {
        list_insert(tail_ptr, source_ptr->data( ));
        tail_ptr = tail_ptr->link( );
        source_ptr = source_ptr->link( );
    }
    }
    
    template <class Item>
    void list_head_insert(node<Item>*& head_ptr, const Item& entry)
    {
    head_ptr = new node<Item>(entry, head_ptr);
    }

    template <class Item>
    void list_head_remove(node<Item>*& head_ptr)
    {
    node<Item> *remove_ptr;

    remove_ptr = head_ptr;
    head_ptr = head_ptr->link( );
    delete remove_ptr;
    }

    template <class Item>
    void list_insert(node<Item>* previous_ptr, const Item& entry) 
    {
    node<Item> *insert_ptr;
    
    insert_ptr = new node<Item>(entry, previous_ptr->link( ));
    previous_ptr->set_link(insert_ptr);
    }

    template <class Item>
    std::size_t list_length(const node<Item>* head_ptr)
    // Library facilities used: cstdlib
    {
    const node<Item> *cursor;
    std::size_t answer;
    
    answer = 0;
    for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( ))
        ++answer;
    
    return answer;
    }

    template <class NodePtr, class SizeType>
    NodePtr list_locate(NodePtr head_ptr, SizeType position) 
    // Library facilities used: cassert, cstdlib
    {
    NodePtr cursor;
    SizeType i;
    
    assert(0 < position);
    cursor = head_ptr;
    for (i = 1; (i < position) && (cursor != NULL); ++i)
        cursor = cursor->link( );
    return cursor;
    }

    template <class Item>
    void list_remove(node<Item>* previous_ptr)
    {
    node<Item> *remove_ptr;

    remove_ptr = previous_ptr->link( );
    previous_ptr->set_link(remove_ptr->link( ));
    delete remove_ptr;
    }

    template <class NodePtr, class Item>
    NodePtr list_search(NodePtr head_ptr, const Item& target) 
    // Library facilities used: cstdlib
    {
    NodePtr cursor;
    
    for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( ))
        if (target == cursor->data( ))
        return cursor;
    return NULL;
    }
}

队列标头

#ifndef MAIN_SAVITCH_QUEUE2_H     // Prevent duplicate definition
#define MAIN_SAVITCH_QUEUE2_H
#include <cstdlib>   // Provides std::size_t
#include "node2.h"   // Node template class

namespace main_savitch_8C
{
    template <class Item>
    class queue
    {
    public:
        // TYPEDEFS 
        typedef std::size_t size_type;
        typedef Item value_type;
        // CONSTRUCTORS and DESTRUCTOR
        queue();
        queue(const queue<Item>& source);
        ~queue();
        // MODIFICATION MEMBER FUNCTIONS
        void pop();
        void push(const Item& entry);
        void operator =(const queue<Item>& source);
        // CONSTANT MEMBER FUNCTIONS
        bool empty() const { return (count == 0); }
        Item front() const;
        size_type size() const { return count; }
    private:
        main_savitch_6B::node<Item>* front_ptr;
        main_savitch_6B::node<Item>* rear_ptr;
        size_type count;       // Total number of items in the queue
    };
}
#include "queue2.template" // Include the implementation

#endif

队列模板

#include <cassert>   // Provides assert
#include "node2.h"   // Node template class

namespace main_savitch_8C
{
    template <class Item>
    queue<Item>::queue( )
    {
        count = 0;
        front_ptr = NULL;
    }

    template <class Item>
    queue<Item>::queue(const queue<Item>& source)
    // Library facilities used: node2.h
    {
    count = source.count;
    list_copy(source.front_ptr, front_ptr, rear_ptr);
    }

    template <class Item>
    queue<Item>::~queue( )
    {
        list_clear(front_ptr);
    }

    template <class Item>
    void queue<Item>::operator =(const queue<Item>& source)
    // Library facilities used: node2.h
    {
        if (this == &source) // Handle self-assignment
            return;
        list_clear(front_ptr);
        list_copy(source.front_ptr, front_ptr, rear_ptr);
        count = source.count;
    }

    template <class Item>
    Item queue<Item>::front( ) const
    // Library facilities used: cassert
    {
        assert(!empty( ));    
        return front_ptr->data( );
    }
    
    template <class Item>
    void queue<Item>::pop( )
    // Library facilities used: cassert, node2.h
    {
        assert(!empty( ));
    list_head_remove(front_ptr);
    --count;
    }
    
    template <class Item>
    void queue<Item>::push(const Item& entry)
    // Library facilities used: node2.h
    {
        if (empty( ))
        {   // Insert first entry.
            list_head_insert(front_ptr, entry);
            rear_ptr = front_ptr;
        }
        else
        {   // Insert an entry that is not the first.
            list_insert(rear_ptr, entry);
            rear_ptr = rear_ptr->link( );
        }
        ++count;
    }

}

进程标头

#ifndef MAIN_SAVITCH_PROCESS_H
#define MAIN_SAVITCH_PROCESS_H

namespace main_savitch_8C
{
    class process
    {
    public:


        double arrival_time;
        int waiting_time;
        double service_time;
        int id;
        int priority_number;
        int order;
        static int nextid;
        static int nextpri;
        double totaltime;
        process(int comp);
        process();

        void arrival();
        int waiting_times();
        void time_insystem();


    };
}
#endif

process implementation

#include <cstdlib>
#include <iostream>
#include <random>
#include "Process.h"


namespace main_savitch_8C
{
    process::process(int comp)
    {

        waiting_time = (rand() % 2400) + 1;
        service_time = (rand() % 2400) + 1;
        if (waiting_time > service_time)
        {
            arrival_time = (rand() % (int)service_time);
        }
        else
        {
            arrival_time = (rand() % waiting_time);
        }
        nextid = 0;
        id = nextid++;
        nextpri = 0;
        priority_number = nextpri++;
        order = comp;
        totaltime = 0;
    }

    process::process()
    {

    }
    int process::waiting_times()
    {
        return waiting_time;
    }
    void process::time_insystem()
    {
        totaltime = (service_time + waiting_time - arrival_time);
    }
}

scheduler header


#ifndef MAIN_SAVITCH_SCHEDULER_H
#define MAIN_SAVITCH_SCHEDULER_H
#include "Process.h"
#include "node.h"
#include "queue2.h"
/*#include "queue.template"*/

namespace main_savitch_8C
{
    class scheduler
    {
    private:
        int queuelength;
    public:

        scheduler();
        queue<process> pqueue;
        void enlist(process p);
        void delist(process  p);
        int set_priority(process p);
        void schedule(process p);
        size_t list_length();
        void statistician();
    };
}
//#include "scheduler.cpp"

#endif

调度程序实现。包括主要

#include <cstdlib>
#include <iostream>
#include "queue2.h"
#include "Scheduler.h"
#include "node.h"
#include "Process.h"


namespace main_savitch_8C
{
    scheduler::scheduler()
    {

    }

    void scheduler::enlist(process q)
    {
        pqueue.push(q);
    }

    void scheduler::delist(process q)
    {
        pqueue.pop();
    }

    int scheduler::set_priority(process q)
    {
        return q.priority_number;
    }

    void scheduler::schedule(process q)
    {
        cout << pqueue.front().id << endl;
        cout << pqueue.front().totaltime << endl;
        cout << pqueue.front().priority_number << endl;
    }


    int main()
    {
        int s;
        scheduler nsched;
        process q = process();
        for (s = 0; s < 1000; s++)
        {
            q = process(s);
            nsched.enlist(q);
            q.totaltime;
            nsched.schedule(q);
            nsched.delist(q);

        }
        return 0;
    }
}

这是我收到的错误

LNK2001 

unresolved external symbol "public: static int main_savitch_8C::process::nextid" (?nextid@process@main_savitch_8C@@2HA) 



LNK2001 

unresolved external symbol "public: static int main_savitch_8C::process::nextpri" (?nextpri@process@main_savitch_8C@@2HA)   



LNK2019 

unresolved external symbol "void __cdecl list_clear<class main_savitch_8C::process>(class node<class main_savitch_8C::process> * &)" (??$list_clear@Vprocess@main_savitch_8C@@@@YAXAAPAV?$node@Vprocess@main_savitch_8C@@@@@Z) referenced in function "public: __thiscall queue<class main_savitch_8C::process>::~queue<class main_savitch_8C::process>(void)" (??1?$queue@Vprocess@main_savitch_8C@@@@QAE@XZ)  




LNK2019 

unresolved external symbol "void __cdecl list_head_remove<class main_savitch_8C::process>(class node<class main_savitch_8C::process> * &)" (??$list_head_remove@Vprocess@main_savitch_8C@@@@YAXAAPAV?$node@Vprocess@main_savitch_8C@@@@@Z) referenced in function "public: void __thiscall queue<class main_savitch_8C::process>::pop(void)" (?pop@?$queue@Vprocess@main_savitch_8C@@@@QAEXXZ)  




LNK2019 

unresolved external symbol "void __cdecl list_head_insert<class main_savitch_8C::process>(class node<class main_savitch_8C::process> * &,class main_savitch_8C::process const &)" (??$list_head_insert@Vprocess@main_savitch_8C@@@@YAXAAPAV?$node@Vprocess@main_savitch_8C@@@@ABVprocess@main_savitch_8C@@@Z) referenced in function "public: void __thiscall queue<class main_savitch_8C::process>::push(class main_savitch_8C::process const &)" (?push@?$queue@Vprocess@main_savitch_8C@@@@QAEXABVprocess@main_savitch_8C@@@Z)



LNK2019 

unresolved external symbol "void __cdecl list_insert<class main_savitch_8C::process>(class node<class main_savitch_8C::process> *,class main_savitch_8C::process const &)" (??$list_insert@Vprocess@main_savitch_8C@@@@YAXPAV?$node@Vprocess@main_savitch_8C@@@@ABVprocess@main_savitch_8C@@@Z) referenced in function "public: void __thiscall queue<class main_savitch_8C::process>::push(class main_savitch_8C::process const &)" (?push@?$queue@Vprocess@main_savitch_8C@@@@QAEXABVprocess@main_savitch_8C@@@Z) 


LNK2019 

unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)   



LNK1120 

7 unresolved externals  
C++ 模板 链接器错误

评论

0赞 Alan Birtles 7/25/2021
请将错误发布为文本而不是屏幕截图,并且绝对不要拍摄糟糕的屏幕照片
0赞 Evg 7/25/2021
您是否正在尝试将模板放入 .cpp 中?
0赞 Jay Harris 7/25/2021
是的,我应该为模板使用什么文件?CXX?
0赞 Alan Birtles 7/25/2021
最好从编译器输出窗口复制错误,而不是从错误列表中复制错误
0赞 Alan Birtles 7/25/2021
读取喜欢的重复项,模板一般只能在头文件中实现,需要定义静态变量

答: 暂无答案