C ++ - 致命错误LNK1120:3个未解析的外部,无法找到解决方案

时间:2017-11-09 19:02:40

标签: c++ list templates linked-list

所以,我的代码出了问题。我只是试图运行一个示例代码来查看它是如何工作的(我是C ++的新手),每次调用List类时都会出现此错误。

以下是代码:

//main.cpp
#include <iostream>
#include "List.h"
using namespace std;

int main() {
    List<char> list;
    // 5 types created using a 'for' loop 
    // and calling the ‘add’ function

    for (int i = 101; i <= 105; i++)
    {
        list.add(char(i));
    }
    list.display();
    // should output a, b, c, d, e 
    // in this example.
    cout << endl;
    return 0;
}

这是List类

//List.h
#pragma once
#include "Link.h"

template <class F> class List
{
private:
    Link<F>* head;
public:
    List();
    int add(F theData);
    void display();
};

我也会把List.cpp提供帮助:

//List.cpp
#include "List.h"

template<typename F> List<F>::List()
{
    // 'head' points to 0 initially and when the list is empty.  
    // Otherwise 'head' points to most recently 
    // added object head
    head = 0;
}
template<typename F> void List<F>::display()
{
    Link<F>* temp;// 'temp' used to iterate through the list
                  // 'head' points to last object added
                  // Iterate back through list until last pointer (which is 0)
    for (temp = head; temp != 0; temp = temp->Next)
    {
        cout << "Value of type is " << temp->X << endl;
    }
}

template<typename F> int List<F>::add(F theData)
{
    // pointer 'temp' used to instantiate objects to add to list 
    Link<F>* temp;

    // memory allocated and the object is given a value
    temp = new Link<F>(theData);
    if (temp == 0) // check new succeeded
    {
        return 0;  // shows error in memory allocation
    }
    // the pointer in the object is set to whatever 'head' is pointing to
    temp->Next = head;
    // 'head' is re-directed to point to the last created object
    head = temp;
    return 1;
}

当我跑步时,它给了我这些错误:

errors

PS。我正在使用VS2017 btw。

谢谢

0 个答案:

没有答案