Monday, May 8, 2017

list - C++ - fatal error LNK1120: 3 unresolved externals, can't find solution

So, I'm having problems with my code. I'm simply trying to run an example code to see how it works (I'm a newbie at C++) and this error is appearing everytime I call the List class.



Here's the code:



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


int main() {
List 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;
}


And here's the List class




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

template class List
{
private:
Link* head;
public:
List();

int add(F theData);
void display();
};


and I'll also put List.cpp to help:



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


template List::List()
{
// 'head' points to 0 initially and when the list is empty.
// Otherwise 'head' points to most recently
// added object head
head = 0;
}
template void List::display()
{
Link* 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 int List::add(F theData)
{

// pointer 'temp' used to instantiate objects to add to list
Link* temp;

// memory allocated and the object is given a value
temp = new Link(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;
}


When I run, It gives me these errors:



errors




PS. I'm using VS2017 btw.



Thank you

No comments:

Post a Comment