在构建项目使用MingGW编译器时如何解决“未定义引用”错误

时间:2019-05-17 10:07:05

标签: c++ visual-studio-code mingw

当我尝试在实现之间分离文件时,我曾用C \ C ++编写合集的实践,但遇到了问题。

当前我有下一个文件

helloworld.cpp

#include <iostream>
#include <string>
#include "LinkedList.h"

using namespace std;

int main()
{
    LinkedList<string> list;
    list.add("I");
    list.add("Simple");
    list.add("Text");
    list.add("Just");
    list.add("For Fun");
}

LinkedList.h

#pragma once
#include <iostream>

using namespace std;

template <typename T>
class LinkedList
{
private:

    class Node
    {
    public:
        Node *next;
        Node *previous;
        T value;
    };

    Node *head = nullptr;
    Node *tail = nullptr;
    int _size = 0;

    void clearAllNodes(Node *node);

public:
    ~LinkedList();

    void add(const T &t);

    bool remove(const T &t);

    T get(int index);

    T getHead();

    T getTail();

    int size();

};

LinkedList.cpp

#include "LinkedList.h"

template <typename T>
LinkedList<T>::~LinkedList()
{

    clearAllNodes(head);
    _size = 0;
    head = nullptr;
    tail = nullptr;
}

template <typename T>
void LinkedList<T>::add(const T &t)
{
    Node *node = new Node();
    node->value = t;
    if (nullptr == head)
    {
        head = node;
        tail = node;
    }
    else
    {
        node->previous = tail;
        tail->next = node;
        tail = node;
    }

    ++_size;
}

template <typename T>
bool LinkedList<T>::remove(const T &t)
{
    if (_size == 0)
        return false;

    Node *node = head;

    do
    {
        if (node->value == t)
        {
            Node *previous = node->previous;
            Node *next = node->next;

            if (nullptr == previous && nullptr != next)
            {
                head = next;
            }
            else if (nullptr != previous && nullptr != next)
            {
                previous->next = next;
                next->previous = previous;
            }
            else if (nullptr != previous && nullptr == next)
            {
                tail = previous;
            }

            return true;
        }
        node = node->next;
    } while (node != nullptr);

    return false;
}

template <typename T>
T LinkedList<T>::get(int index)
{
    if (_size == 0)
        return NULL;

    Node *node = head;

    for (int i = 0; i < _size; i++)
    {
        if (nullptr == node)
        {
            break;
        }
        if (i == index)
        {
            return node->value;
        }
        node = node->next;
    }
    return NULL;
}

template <typename T>
T LinkedList<T>::getHead()
{
    return nullptr == head ? NULL : head->value;
}

template <typename T>
T LinkedList<T>::getTail()
{
    return nullptr == tail ? NULL : tail->value;
}

template <typename T>
int LinkedList<T>::size()
{
    return _size;
}

template <typename T>
void LinkedList<T>::clearAllNodes(Node *node)
{
    if (nullptr == node)
        return;

    if (node->next != nullptr)
        clearAllNodes(node->next);

    delete node;
}

我也使用 VS Code ,而我的 tasks.json 包含下一个配置

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build project",
            "type": "shell",
            "command": "g++",
            "args": [
                "-o",
                "helloworld",
                "helloworld.cpp",
                "LinkedList.cpp",
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

问题是,当我尝试构建项目时遇到下一个错误

..\AppData\Local\Temp\ccs2v33S.o:helloworld.cpp:(.text+0x5f): undefined reference to `LinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::add(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
..\AppData\Local\Temp\ccs2v33S.o:helloworld.cpp:(.text+0xad): undefined reference to `LinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::add(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
..\AppData\Local\Temp\ccs2v33S.o:helloworld.cpp:(.text+0xfb): undefined reference to `LinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::add(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
..\AppData\Local\Temp\ccs2v33S.o:helloworld.cpp:(.text+0x155): undefined reference to `LinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::add(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
..\AppData\Local\Temp\ccs2v33S.o:helloworld.cpp:(.text+0x1b5): undefined reference to `LinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::add(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
..\AppData\Local\Temp\ccs2v33S.o:helloworld.cpp:(.text+0x270): undefined reference to `LinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::~LinkedList()'
..\AppData\Local\Temp\ccs2v33S.o:helloworld.cpp:(.text+0x356): undefined reference to `LinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::~LinkedList()'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1

任何人都可以解释一下,我在项目中错过了什么吗?谢谢

0 个答案:

没有答案