0

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 <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;
}

And here's the List class

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

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

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

//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;
}

When I run, It gives me these errors:

errors

PS. I'm using VS2017 btw.

Thank you

  • More detailed explanation of the problem: [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – user4581301 Nov 09 '17 at 19:10
  • You closed my question, But I still can't find the answer to my problem... – Dário Hermann Nov 09 '17 at 19:25
  • This is covered in more detail in the question linked above, but a template must be specialized to be useful. The compiler will expand the template into real class based on the specialization. In order to do this it must be able to find ALL of the template. Because you hid the function definitions in List.cpp, when the template is expanded main.cpp the functions are not expanded because they can't be found. Later the linker goes looking for these missing functions to build the program and also cannot find them because nothing in link.cpp told the compiler to expand the functions. – user4581301 Nov 09 '17 at 19:32
  • Do not try to solve this problem by including list.cpp in list.has this often results in more and different errors. Instead move the function implementations into list.h where anyone who needs to work with list can find and expand them. There are other alternatives, but they can get messy. If you want to know about these other alternatives, they will be covered in detail in [credible intermediate programming texts](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user4581301 Nov 09 '17 at 19:36

0 Answers0