error LNK2019: unresolved external symbol in my Template class [duplicate] - c++

This question already has answers here:
Why do I get "unresolved external symbol" errors when using templates? [duplicate]
(3 answers)
Why can templates only be implemented in the header file?
(17 answers)
Why do C++ template definitions need to be in the header? [duplicate]
(5 answers)
Closed 9 years ago.
I have created a list and it worked perfectly, but I have to rewrite it using template, I have done this. But when I trying to compile my code I get Linker Error!
Can you help with this code? Any help would be much appreciated!
This is List.h
#pragma once
#include <cstdlib>
template <class T>
class List
{
public:
struct Node
{
T data;
Node *next, *prev;
Node();
Node(T &smth);
~Node();
};
Node *head;
Node *tail;
public:
List();
~List();
void add(T &d);
void del(Node *);
};
List.cpp
#include "List.h"
template <class T>
List<T>::List()
{
}
template <class T>
List<T>::~List()
{
Node*n;
while (head != nullptr)
{
n = head->prev;
delete head;
head = n;
}
}
template <class T>
void List<T>::add(T &d)
{
Node *n = new Node(d);
n->prev = head;
n->next = nullptr;
if (head!=nullptr)
head->next = n;
if(head == nullptr){
head = n;
tail = head;
} else
head = n;
}
template <class T>
void List<T>::del(Node *node)
{
if (head == node)
head = node->prev;
if (tail == node)
tail = node->next;
if (node->next != nullptr)
node->next->prev = node->prev;
if (node->prev != nullptr)
node->prev->next = node->next;
delete node;
}
template <class T>
List<T>::Node::Node():data(),next(nullptr),prev(nullptr){}
template <class T>
List<T>::Node::Node(T &smth):data(smth),next(nullptr),prev(nullptr){}
template <class T>
List<T>::Node::~Node(){
data.~Word();
next = nullptr;
prev = nullptr;
}
and main.cpp
#include <List.h>
#include <iostream>
using namespace std;
int main()
{
int a;
List<int> my;
cin >> a;
my.add(a);
cin >> a;
my.add(a);
cin >> a;
my.add(a);
}
and I get these errors:
Error 1 error LNK2019: unresolved external symbol "public: __thiscall >List::List(void)" (??0?$List#H##QAE#XZ) referenced in function _main >c:\Users\lapchenko\documents\visual studio >2012\Projects\ConsoleApplication1\ConsoleApplication1\Source.obj
Error 2 error LNK2019: unresolved external symbol "public: __thiscall >List::~List(void)" (??1?$List#H##QAE#XZ) referenced in function _main >c:\Users\lapchenko\documents\visual studio >2012\Projects\ConsoleApplication1\ConsoleApplication1\Source.obj
Error 3 error LNK2019: unresolved external symbol "public: void __thiscall >List::add(int &)" (?add#?$List#H##QAEXAAH#Z) referenced in function _main >c:\Users\lapchenko\documents\visual studio >2012\Projects\ConsoleApplication1\ConsoleApplication1\Source.obj
Error 4 error LNK1120: 3 unresolved externals c:\users\lapchenko\documents\visual >studio 2012\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe 1

Related

Getting an error if not using curly braces with the constructor of a class when including it through header file

I'm calling the default constructor and i'm confused why this gives an error
struct linkedlist
{
node* head = nullptr;
linkedlist();
linkedlist(vector <int> arr)
{
insert_nodes(arr);
}
....
1>Source.obj : error LNK2019: unresolved external symbol "public: __thiscall linkedlist::linkedlist(void)" (??0linkedlist##QAE#XZ)
but if I add curly braces after the constructor it runs fine
struct linkedlist
{
node* head = nullptr;
linkedlist()
{
}
linkedlist(vector <int> arr)
{
insert_nodes(arr);
}
....

LNK2019: Unresolved external symbol error - with member function of class template

I am trying to create a custom List in c++.
I've defined it this way:
List.h:
#include "ListItem.h"
#pragma once
template<class T> class List
{
private:
ListItem<T>* first;
public:
T* GetAt(int);
ListItem<T>* GetLastListItem();
void Add(T*);
void Clear();
};
List.cpp:
#include "stdafx.h"
#include "List.h"
template<class T> T* List<T>::GetAt(int index)
{
if (!first)
return 0;
ListItem<T>* current = first;
for (int i = 1; i < index; i++)
{
current = current->GetNext();
}
return current->GetItem();
}
template<class T> L...
main:
List<TestItem> liste;
TestItem ti; //just a int inside.
liste.Add(&ti);
I am getting the following errors:
1>ConsoleApplication1.obj : error LNK2019: unresolved external symbol ""public: void __thiscall List::Add(class TestItem *)" (?Add#?$List#VTestItem####QAEXPAVTestItem###Z)" in function"_main".

C++ linker error - cannot see why its not linking? (full complete code example included) [duplicate]

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 8 years ago.
This is my code and I am getting a linking problem on the constructor:
#ifndef LOCKFREEQUEUE_H
#define LOCKFREEQUEUE_H
#include <atomic>
#include <memory>
template<typename T>
class lock_free_queue
{
private:
struct node
{
std::shared_ptr<T> data;
node* next;
node():next(nullptr){}
};
std::atomic<node*> head;
std::atomic<node*> tail;
node* pop_head()
{
node* const old_head=head.load();
if(old_head==tail.load())
{
return nullptr;
}
head.store(old_head->next);
return old_head;
}
lock_free_queue(const lock_free_queue& other);
lock_free_queue& operator=(const lock_free_queue& other);
public:
lock_free_queue();
~lock_free_queue();
std::shared_ptr<T> pop();
void push(T new_value);
};
Source file:
#include "lock_free_queue.h"
template<typename T>
lock_free_queue<T>::lock_free_queue(const lock_free_queue& other)
{
}
template<typename T>
lock_free_queue<T>& lock_free_queue<T>::operator=(const lock_free_queue& other)
{
}
template<typename T>
lock_free_queue<T>::lock_free_queue():head(new node),tail(head.load())
{
}
template<typename T>
lock_free_queue<T>::~lock_free_queue()
{
while(node* const old_head=head.load())
{
head.store(old_head->next);
delete old_head;
}
}
template<typename T>
std::shared_ptr<T> lock_free_queue<T>::pop()
{
node* old_head=pop_head();
if(!old_head)
{
return std::shared_ptr<T>();
}
std::shared_ptr<T> const res(old_head->data);
delete old_head;
return res;
}
template<typename T>
void lock_free_queue<T>::push(T new_value)
{
std::shared_ptr<T> new_data(std::make_shared<T>(new_value));
node* p=new node;
node* const old_tail=tail.load();
old_tail->data.swap(new_data);
old_tail->next=p;
tail.store(p);
}
Main:
#include "lock_free_queue.h"
int main(){
lock_free_queue<int>* my_queue = new lock_free_queue<int>();
my_queue->push(3);
return 1;
}
Linker error:
Main.obj : error LNK2019: unresolved external symbol "public: __cdecl lock_free_queue<int>::lock_free_queue<int>(void)" (??0?$lock_free_queue#H##QEAA#XZ) referenced in function main
Main.obj : error LNK2019: unresolved external symbol "public: void __cdecl lock_free_queue<int>::push(int)" (?push#?$lock_free_queue#H##QEAAXH#Z) referenced in function main
I can't see why these aren't linking?
The template class definition and its member function realizations shall be in one header file.

Unique pointer in Linked List

I was trying to create a Linked List by using unique pointer. However, my program does not compile due to some weird errors that I do not know how to fix it. Would anyone please help me out how to solve this problem ? Thank you.
ContactList.h
#pragma once
#include"Contact.h"
#include<memory>
using namespace std;
class ContactList
{
public:
ContactList();
~ContactList();
void addToHead(const std::string&);
void PrintList();
private:
//Contact* head;
unique_ptr<Contact> head;
int size;
};
ContactList.cpp
#include"ContactList.h"
#include<memory>
using namespace std;
ContactList::ContactList(): head(new Contact()), size(0)
{
}
void ContactList::addToHead(const string& name)
{
//Contact* newOne = new Contact(name);
unique_ptr<Contact> newOne(new Contact(name));
if(head == 0)
{
head.swap(newOne);
//head = move(newOne);
}
else
{
newOne->next.swap(head);
head.swap(newOne);
//newOne->next = move(head);
//head = move(newOne);
}
size++;
}
void ContactList::PrintList()
{
//Contact* tp = head;
unique_ptr<Contact> tp(new Contact());
tp.swap(head);
//tp = move(head);
while(tp != 0)
{
cout << *tp << endl;
tp.swap(tp->next);
//tp = move(tp->next);
}
}
These are the errors that I've got:
Error 1 error LNK2019: unresolved external symbol "public: __thiscall ContactList::~ContactList(void)" (??1ContactList##QAE#XZ) referenced in function "public: void * __thiscall ContactList::`scalar deleting destructor'(unsigned int)" (??_GContactList##QAEPAXI#Z) E:\Fall 2013\CPSC 131\Practice\Practice\Practice\ContactListApp.obj
Error 2 error LNK1120: 1 unresolved externals E:\Fall 2013\CPSC 131\Practice\Practice\Debug\Practice.exe 1
Your ContactList destructor has no implementation.
Add to ContactList.cpp
ContactList::~ContactList()
{
}
Or (since the destructor is trivial anyway), just remove the explicit destructor from the class definition:
class ContactList
{
public:
ContactList();
// no explicit destructor required
void addToHead(const std::string&);
void PrintList();
private:
unique_ptr<Contact> head;
int size;
};

Visual Studio 2010 Linker Error C++ when declaring a class I created from a header file

I'm implementing a doubly linked list just for fun and for a refresher and getting a strange linker error when I simply try to declare an instance of it in my main function.
The error is:
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall
d_list<int>::d_list<int>(void)" (??0?$d_list#H##QAE#XZ) referenced in function _main
1>c:\users\ben\documents\visual studio 2010\Projects\dlist\Debug\dlist.exe : fatal
error LNK1120: 1 unresolved externals
main.cpp is just:
#include "d_list.h"
int main(){
d_list<int> test;
return 0;
}
The structure itself is unfinished, but I'll paste the code here just in case you guys need it.
Header file:
#ifndef D_LIST_H
#define D_LIST_H
template <class T>
class d_list{
public:
d_list();
d_list(const d_list &_dl);
void push_back(T item);
void push_front(T item);
void pop_back();
void pop_front();
private:
struct node{
node *prev;
node *next;
T data;
};
node *head;
node *tail;
int size;
};
#endif //D_LIST_H
d_list.cpp:
#include "d_list.h"
template <class T>
d_list<T>::d_list(){
}
template <class T>
d_list<T>::d_list(const d_list<T> &_dl){
}
template <class T>
void d_list<T>::push_back(T item){
node *new_tail = new node;
new_tail->data = item;
new_tail->next = head;
new_tail->prev = tail;
tail->next = new_tail;
size++;
}
template <class T>
void d_list<T>::push_front(T item){
node *new_head = new node;
new_head->data = item;
new_head->prev = tail;
new_head->next = head;
head->prev = new_head;
size++;
}
template <class T>
void d_list<T>::pop_back(){
node *temp = tail->prev;
temp->next = head;
delete tail;
tail = temp;
temp = NULL;
}
template <class T>
void d_list<T>::pop_front(){
}
template and its implementation should be defined in header file.
They are not normal functions or class as they are compiled on demand.
Try to remove .cpp and put all the implementation on header file