Apple mach-o linker (id) error with C++ - c++

I created a new project in Xcode 5.0 to implement a Queue class as following:
I created a .h file called QueueArray.h and it contains the following:
#ifndef __Queue__QueueArray__
#define __Queue__QueueArray__
#include <iostream>
template <class T>
class QueueArray
{
public:
QueueArray(int cap);
~QueueArray();
void Enqueue(T& val);
T Dequeue(void);
T GetFirst(void);
T GetLast(void);
bool IsEmpty(void);
bool IsFull(void);
void Clear(void);
private:
T* data;
int capacity, size, first, last;
};
#endif
and a .cpp file called QueueArray.cpp that contains the following:
#include "QueueArray.h"
using namespace std;
template <class T>
QueueArray<T>::QueueArray(int cap)
{
capacity = cap;
data = new T[capacity];
size = 0;
first = last = -1;
}
template <class T>
QueueArray<T>::~QueueArray(void)
{
delete [] data;
}
template <class T>
void QueueArray<T>::Enqueue(T& el)
{
if(IsFull() == true)
{
printf("\n Can't enqueue into a full queue!");
return;
}
if(IsEmpty() == true)
first = last = 0;
else if(last == capacity-1)//if at the last entry
last = 0; //wrap around to the first entry
else
last++;
data[last] = el;
size++;
}
template <class T>
T QueueArray<T>::Dequeue()
{
if(IsEmpty() == true)
{
printf("\n Can't dequeue from an empty queue!");
return -1;
}
T el = data[first];
if(first == last) //if only one element in queue
last = first = -1; //we'll get an empty queue
else if(first == capacity-1) //if at the last entry
first = 0; //wrap around to the first entry
else //normal case
first++;
size--;
return el;
}
template <class T>
T QueueArray<T>::GetFirst()
{
return data[first];
}
template <class T>
T QueueArray<T>::GetLast()
{
return data[last];
}
template <class T>
bool QueueArray<T>::IsEmpty(void)
{
return size == 0;
}
template <class T>
bool QueueArray<T>::IsFull(void)
{
return size == capacity;
}
and a main.cpp file that contains the following:
#include <iostream>
#include "QueueArray.h"
using namespace std;
int main(int argc, const char * argv[])
{
QueueArray<int> q(100);
for (int i=0; i<100; i++)
{
q.Enqueue(i);
}
for (int i=0; i<100; i++) {
cout<<q.Dequeue()<<endl;
}
return 0;
}
When I try to run the project, a message appears saying "Build Failed" and here is a screenshot of the errors:
How to fix that?

The compiler needs to see the definition of the Template before using it.
http://www.parashift.com/c++-faq-lite/templates-defn-vs-decl.html
There are more than one solution:
a. Move the definitions from .cpp file to the .h file
b. Use the export keyword if your compiler supports
http://www.parashift.com/c++-faq-lite/separate-template-fn-defn-from-decl-export-keyword.html
c. Add an inline declaration:
http://www.parashift.com/c++-faq-lite/separate-template-fn-defn-from-decl.html

You need to include the template definitions in-line with their declarations. An easy solution to this is to rename QueueArray.cpp to something like QueueArray_impl.h and #include it from the bottom of QueueArray.h
#ifndef __Queue__QueueArray__
#define __Queue__QueueArray__
#include <iostream>
template <class T>
class QueueArray
{
public:
QueueArray(int cap);
~QueueArray();
void Enqueue(T& val);
T Dequeue(void);
T GetFirst(void);
T GetLast(void);
bool IsEmpty(void);
bool IsFull(void);
void Clear(void);
private:
T* data;
int capacity, size, first, last;
};
#include "QueueArray_impl.h"
#endif

Related

Implement a template of a Queue, using 2 Classes [duplicate]

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed last year.
I have a problem. I have implemented a Queue, while using a Class "Queue" and a Class "Element".
the problem i have now is, that I can't work out how to create the template for class Element.
If I don't use the template and just use int instead of T. Everything works fine. I already looked for many examples on the Internet. But nobody uses two classes, which is probably more efficient. I think my problem is that i don't know how to use the pointer in templates.
PS: The template in Queue.h and.cpp works i think, but if i start trying to create a template for Element it doesn't work.
My Queue.h file
#ifndef ELEMENT_H
#define ELEMENT_H
#include "Element.h"
template <class T>
class Queue{
public:
explicit Queue(int max_queue);
~Queue() = default;
void enqueue(T inhalt);
Element* dequeue();
Element* show();
bool isEmpty();
private:
Element<T>* front{};
Element<T>* back{};
int max;
int anzahl = 0;
};
#endif
My Queue.cpp file
#include "Queue.h"
#include <iostream>
#include <string>
template <class T>
Queue<T>::Queue(int max_queue){
max = max_queue;
}
template <class T>
void Queue<T>::enqueue(T inhalt){
Element* e = new Element(inhalt);
if(max > anzahl){
if(isEmpty()){
front = back = e;
}else{
back->setNext(e);
back = e;
}
} anzahl++;
}
template <class T>
Element* Queue<T>::dequeue(){
Element* e = front;
front = front->getNext();
e->setNext(nullptr);
return e;
}
template <class T>
bool Queue<T>::isEmpty()
{
return anzahl == 0;
}
template <class T>
Element* Queue<T>::show()
{
return front;
}
My Element.h file
#ifndef QUEUE_H
#define QUEUE_H
class Element{
public:
explicit Element(int);
~Element() = default;
int getInhalt()const;
void setInhalt(int);
Element*getNext()const;
void setNext(Element*);
protected:
int inhalt;
Element* next;
};
#endif
My Element.cpp file
#include <string>
#include "Element.h"
Element::Element( int inhalt_element )
{
inhalt = inhalt_element;
next = nullptr;
}
int Element::getInhalt() const {
return inhalt;
}
void Element::setInhalt(int inhalt) {
Element::inhalt = inhalt;
}
Element* Element::getNext() const {
return next;
}
void Element::setNext(Element *next) {
Element::next = next;
}
The warning I'm getting is main.cpp:(.text+0x1a): undefined reference to `Queue::Queue(int)'
And If I try to use a template for the element class. There are hundreds of lines in the warnings, so I know I'm thinking completely wrong.
I am still pretty novice if it is about programming so any help or any idea would be really appreciated.
Thank you
The implementation of template classes must be done in the .h
The following code compiles.
#include <iostream>
#include <string>
template <class T>
class Element{
public:
Element( T inhalt_element ){
inhalt = inhalt_element;
next = nullptr;
}
T getInhalt() const {
return inhalt;
}
void setInhalt(T inhalt) {
this->inhalt = inhalt;
}
Element<T>* getNext() const {
return next;
}
void setNext(Element<T> *next) {
this->next = next;
}
protected:
T inhalt;
Element<T>* next;
};
template <class T>
class Queue{
public:
Queue(int max_queue){
max = max_queue;
}
void enqueue(T inhalt){
Element<T>* e = new Element<T>(inhalt);
if(max > anzahl){
if(isEmpty()){
front = back = e;
}else{
back->setNext(e);
back = e;
}
} anzahl++;
}
Element<T>* dequeue(){
Element<T>* e = front;
front = front->getNext();
e->setNext(nullptr);
return e;
}
bool isEmpty(){
return anzahl == 0;
}
Element<T>* show(){
return front;
}
private:
Element<T>* front{};
Element<T>* back{};
int max;
int anzahl = 0;
};
int main() {
std::cout << "Hello World!";
Queue<int> queue(10);
return 0;
}

Getting a segmentation fault when trying to add vertex to custom Graph data structure?

I am trying to create a custom Graph data structure in C++. I am using std::set to keep track of the vertices. I check the set when I insert a new vertex to make sure it is not already in the graph. My experience with C++ is minimal, so I am having trouble implementing this properly. The problem seems to be with the contains function. Here is the code for the graph:
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#ifndef GRAPH_H
#define GRAPH_H
/*
Graph - Templated graph class
*/
template <typename T>
struct AdjListNode {
T data;
int index;
struct AdjListNode<T> *next;
};
template <typename T>
struct AdjList {
struct AdjListNode<T> *head;
};
template <class T>
class Graph {
private:
std::vector< AdjList<T>* > m_adj_list; //Adjacency list
std::set<T> m_node_set;
std::map<T, int> m_index_map;
int m_num_vertices;
bool m_is_directed;
void addDirectedEdge(T src, T dest);
bool contains(T vertex);
public:
Graph(bool isDirected=true);
bool isDirected();
int numVertices();
bool addVertex(T vertex);
void addEdge(T src, T dest);
std::string printGraph();
AdjListNode<T>* getAdjacent(T vertex);
};
#endif
CPP file:
#include <iostream>
#include <vector>
#include <list>
#include <set>
#include "Graph.h"
template <class T>
Graph<T>::Graph(bool is_directed) {
m_num_vertices = 0;
m_is_directed = is_directed;
}
template <class T>
bool Graph<T>::isDirected() {
return m_is_directed;
}
template <class T>
int Graph<T>::numVertices() {
return m_num_vertices;
}
template <class T>
bool Graph<T>::addVertex(T vertex) {
//check to make sure node is not in graph
if (contains(vertex)) { //segfault occurs here
AdjListNode<T> *newNode = new AdjListNode<T>;
newNode->index = m_adj_list.size();
AdjList<T> *newAdjList = new AdjList<T>;
newAdjList->head = newNode;
m_adj_list.push_back(newAdjList);
m_num_vertices++;
return true;
} else {
return false;
}
}
template <class T>
void Graph<T>::addDirectedEdge(T src, T dest) {
if (contains(src)) {
AdjListNode<T> *newNode = new AdjListNode<T>;
newNode->data = dest;
newNode->next = NULL;
AdjList<T> *list = m_adj_list[m_index_map[src]];
AdjList<T> *currentNode = list->head;
while (currentNode->next != NULL) {
currentNode = currentNode->next;
}
//insert at the end of adjacency list
currentNode->next = newNode;
}
}
template <class T>
bool Graph<T>::contains(T vertex) {
return m_node_set.find(vertex) == vertex;
}
template <class T>
void Graph<T>::addEdge(T src, T dest) {
addDirectedEdge(src, dest);
if (!m_is_directed) {
addDirectedEdge(dest, src);
}
}
template <class T>
std::string Graph<T>::printGraph() {
return "";
}
template <class T>
AdjListNode<T>* Graph<T>::getAdjacent(T vertex) {
if (*m_node_set.find(vertex) == vertex) {
AdjListNode<T>* list = m_adj_list[m_index_map[vertex]];
return list->head->next;
} else {
return NULL;
}
}
Can anyone point me in the right direction to fix this issue? Thanks.
In the contains method, shouldn't it be
return (m_node_set.find(vertex) != m_node_set.end());

How do I overload an operator for a template class in C++?

I have a binary search tree class (BST.h) and a node class (Node.h) of which works fine when I store data types such as integers in it. My problem is trying store class objects in my BST and use an attribute from the object as the key. My program also has a student class which contains studentID and studentName. How would I write an operator overload in my student class so every time my BST preforms operation on nodes, it will overload to the student.getID(), instead of operating on the object itself. I have the rough idea of what the overload function should look like but i don't know where it should go or if its coded correctly anyway.
//My attempt at an operator overload
bool operator< (const Student &s1, const Student &s2)
{
return s1.GetID < s2.GetID;
}
//Node.h
#ifndef NODE_H
#define NODE_H
#include <iostream>
using namespace std;
template<class T>
class Node
{
public:
Node();
T data;
Node *left;
Node *right;
Node(T);
};
template<class T>
Node<T>::Node()
{
}
template<class T>
Node<T>::Node(T d)
{
data = d;
left = NULL;
right = NULL;
}
#endif //
//BST.h
#ifndef BST_H
#define BST_H
#include <iostream>
#include "Node.h"
#include <string>
using namespace std;
template<class T>
class BST
{
public:
BST();
void Insert(T);
Node<T> *Search(T);
void preOrder();
void inOrder();
void postOrder();
~BST();
private:
Node<T> *root;
void Insert(T , Node<T> *aNode);
Node<T> *Search(T, Node<T> *aNode);
void preOrder(Node<T> *aNode);
void inOrder(Node<T> *aNode);
void postOrder(Node<T> *aNode);
};
template<class T>
BST<T>::BST()
{
root = NULL;
}
template<class T>
void BST<T>::Insert(T data, Node<T> *aNode)
{
if (data < aNode->data)
{
if (aNode->left != NULL)
{
Insert(data, aNode->left);
}
else
{
aNode->left = new Node<T>(data);
aNode->left->left = NULL;
aNode->left->right = NULL;
}
}
else
{
if (data >= aNode->data)
{
if (aNode->right != NULL)
{
Insert(data, aNode->right);
}
else
{
aNode->right = new Node<T>(data);
aNode->right->left = NULL;
aNode->right->right = NULL;
}
}
}
}
template<class T>
void BST<T>::Insert(T data)
{
if (root != NULL)
{
Insert(data, root);
}
else
{
root = new Node<T>(data);
root->left = NULL;
root->right = NULL;
}
}
template<class T>
Node<T>* BST<T>::Search(T data, Node<T> *aNode)
{
if (aNode != NULL)
{
if (data == aNode->data)
{
return aNode;
}
if (data < aNode->data)
{
return Search(data, aNode->left);
}
else
{
return Search(data, aNode->right);
}
}
else
{
return NULL;
}
}
template<class T>
Node<T>* BST<T>::Search(T data)
{
return Search(data, root);
}
template<class T>
void BST<T>::preOrder()
{
preOrder(root);
}
template<class T>
void BST<T>::preOrder(Node<T> *aNode)
{
if (aNode != NULL)
{
cout << aNode->data << " ";
preOrder(aNode->left);
preOrder(aNode->right);
}
}
template<class T>
void BST<T>::inOrder()
{
inOrder(root);
}
template<class T>
void BST<T>::inOrder(Node<T> *aNode)
{
if (aNode != NULL)
{
inOrder(aNode->left);
cout << aNode->data << " ";
inOrder(aNode->right);
}
}
template<class T>
void BST<T>::postOrder()
{
postOrder(root);
}
template<class T>
void BST<T>::postOrder(Node<T> *aNode)
{
if (aNode != NULL)
{
postOrder(aNode->left);
postOrder(aNode->right);
cout << aNode->data << " ";
}
}
template<class T>
BST<T>::~BST()
{
}
#endif // !BST_H
//Student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
Student();
Student(string, int);
~Student();
int Student::GetID();
private:
string name;
int ID;
};
inline int Student::GetID()
{
return ID;
}
You seem to be asking about operator< taking Students , however Student is not a class template, so the title of your post is baffling.
As someone else pointed out, your operator< is almost correct, except you have to actually call GetID() instead of comparing pointers to member functions.
This won't work yet until you fix GetID however. Instead of int Student::GetID(); it should be:
int GetID() const;
The const means that it can be called on objects passed by const reference, as you have in your operator< implementation. And you don't repeat the Student:: when declaring functions inside the class. (You use it when defining class members outside of the class definition).
Declare it as a friend function within your Student class, next to the rest of your member functions
friend bool operator < (Student& s1, Student& s2);
Your implementation is correct, it should go outside your Student class within the same header file.

undefined reference general errors

I'm supposed to implement the functionality of a stack and I was given the main.cpp file and was left to write the other files, namely StackofNodes.hpp & .h. These depend on two other files Node.hpp & .h. When I try to build the program I get errors on the main.cpp file that was given to me. This are:
obj/Debug/main.o||In function `main':|
|6|undefined reference to `StackOfNodees<int>::StackOfNodees()'|
|12|undefined reference to `StackOfNodees<int>::push(int)'|
|17|undefined reference to `StackOfNodees<int>::size() const'|
|24|undefined reference to `StackOfNodees<int>::pop()'|
main.cpp
#include <iostream> //std::cout std::cin
#include "StackOfNodees.h" //StackOfNodes
int main()
{
StackOfNodees<int> myStack; //Create an empty stack
int sizeOfStack; //int we'll use later to store the size of the stack
//push some numbers onto the stack
for(int i = 1; i <= 10; i++)
{
myStack.push( i * 5 );
}
//Store the size of the stack before popping anything
sizeOfStack = myStack.size();
std::cout << "There are " << sizeOfStack << " items on the stack" << std::endl;
//Think about why we don't use i<myStack.size()
for(int i = 0; i < sizeOfStack; i++)
{
std::cout << "Popping the top: " << myStack.pop() << std::endl;
}
/* while(!myStack.isEmpty()) is another valid way to pop all the contents of the stack */
}
StackOfNodees.hpp
#ifndef STACKOFNODEES_HPP_INCLUDED
#define STACKOFNODEES_HPP_INCLUDED
StackOfNodees::StackOfNodees()
{
m_top=nullptr;
m_size=0;
}
bool StackOfNodees::isEmpty() const
{
if (m_size==0)
{
return true;
}
return false;
}
int StackOfNodees::size() const
{
return m_size;
}
void StackOfNodees::push(int value)
{
Node* Node1 = new Node;
Node1 -> m_previous = m_top;
m_top=Node1;
Node1 -> m_value = value;
++m_size;
}
int StackOfNodees::pop()
{
// initialize
int returnval = 0;
if (!isEmpty())
{
// fetch the value from the top one
returnval = m_top -> m_value;
Node* temp = m_top;
//move m_top down to previous box.
m_top = m_top -> m_previous;
//delete the popped one
delete temp;
m_size--;
}
else
{
// you may want to throw an exception here for popping a empty stack
}
return returnval;
}
#endif // STACKOFNODEES_HPP_INCLUDED
Node.h
#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED
template<typename T>
Node<T>::Node()
{
Node.setPrevious(nullptr);
Node.setValue();
}
//initiation of getters
template<typename T>
T Node::getValue()
{
return m_value;
}//end getValue
template<typename int>
int Node::getPrevious()
{
return m_previous;
}//end getPrevious
//initiation of setters
template<typename void>
void Node::setValue(T value)
{
m_value = value;
}//end setValue
template<typename void>
void Node::setPrevious(Node<T>* previous)
{
Node<T>* previous = m_previous;
}//end setPrevious
StackOfNodees.h
#ifndef STACKOFNODEES_H_INCLUDED
#define STACKOFNODEES_H_INCLUDED
#include "Node.h"
template<typename T>
class StackOfNodees
{
private:
Node<T>* m_top;
int m_size;
public:
StackOfNodees();
bool isEmpty() const;
int size() const;
void push(T value);
T pop();
};
#endif // STACKOFNODEES_H_INCLUDED
Any help would be much appreciated. Thanks!
You do not have methods of template class StackOfNodees defined.
You need to have
template<typename T>
StackOfNodees<T>::StackOfNodees()
{
m_top=nullptr;
m_size=0;
}
Rather than
StackOfNodees::StackOfNodees()
{
m_top=nullptr;
m_size=0;
}
Change all definitions and move them into StackOfNodees.h file from .hpp. You do not need an extra .hpp file.
Example:
// File a.h
template<typename T>
class A
{
public:
A();
// the rest of the class ....
};
template<typename T>
A<T>::A()
{
// ...
}
file app.cpp
#include "a.h"
void main()
{
A<int> a;
...
}
An extra .hpp file makes code less readable as it breaks reading flow. If you are still required to split your code in that manner then you need to include .hpp into .h file with #include "StackOfNodes.hpp" instruction in the bottom of .h file (a rather unconventional approach).
When you are writing a template class, you must provide definition of the template class in the same file where the declaration itself lies, so that the compiler can instantiate the actual class when compiling.
In this specific case, put the definitions in StackOfNodees.hpp back to the template class declaration in the file StackOfNodees.h after changing this int specific one to general type T one. This is a simple tutorial about the format of a template class.

c++ linking error for template, using only header files, why?

I know there are a lot of similar questions out there - believe me, I've read them - but I can't get this to work. Which is peculiar, because I resolved a similar struggle with a related program just the other day. I realize that the answer to my question quite likely is out there somewhere, but I've spent a good hour or two looking, without much success.
I am trying to build a linked list. The program consists of four files - header files for the linked list and the node, as well as an interace to the list, and the .cpp file containing the main method.
ListTester.cpp
#include "StdAfx.h"
#include "LinkedList.h"
#include <iostream>
#include <string>
using namespace std;
template <typename T>
void main() {
LinkedList<int> a;
a.addFirst(22);
a.addFirst(24);
a.addFirst(28);
LinkedList<int> b;
b = a;
b = b + a;
b += a;
cout<<b;
}
LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Node.h"
#include "List.h"
#include <ostream>
template <typename T>
class LinkedList : public List {
private:
int n;
Node<T> *first;
Node<T> *last;
public:
LinkedList();
LinkedList(const LinkedList & ll);
~LinkedList();
int size();
void clear();
void addFirst(T data);
void addLast(T data);
T removeFirst();
T removeLast();
T getFirst();
T getLast();
Node<T>* getFirstNode() const;
void addAt(int pos, T data);
T removeAt(int pos);
T getAt(int pos);
LinkedList& operator=(const LinkedList<T> &right);
T operator[](int i);
LinkedList& operator+(const LinkedList<T> &right);
LinkedList& operator+=(const LinkedList<T> &right);
friend std::ostream& operator<<(std::ostream &os, const LinkedList<T> & ll);
};
template <typename T>
LinkedList<T>::LinkedList() {
this->n = 0;
this->first = 0;
this->last = 0;
}
template <typename T>
LinkedList<T>::LinkedList(const LinkedList & ll) {
this-> n = 0;
this-> first = 0;
this-> last = 0;
Node *temp = ll.first;
while(temp) {
addLast(temp->getData());
temp = temp->getNext();
}
}
template <typename T>
void LinkedList<T>::addFirst(T data) {
Node *p = new Node(data, first);
first = p;
if(!n)
last = p;
n++;
}
template <typename T>
void LinkedList<T>::addLast(T data) {
Node *p = new Node(data, 0);
if(!n)
first = last = p;
else {
last->next = p;
last = p;
}
n++;
}
template <typename T>
T LinkedList<T>::removeFirst() {
T a = 0;
if(!n)
throw "Can't retrieve element from empty list!";
a = first->getData();
Node *p = first->next;
delete first;
first = p;
n--;
return a;
}
template <typename T>
T LinkedList<T>::removeLast() {
T a = 0;
if(!n)
throw "Can't retrieve element from empty list!";
if(n == 1) {
a = last->getData();
delete first;
first = last = 0;
}
else {
a = last->getData();
Node *p = first;
while(p->next->next != 0)
p = p->next;
delete p->next;
p->next = 0;
last = p;
}
n--;
return a;
}
template <typename T>
T LinkedList<T>::getFirst() {
if(n < 1)
throw "Can't retrieve element from empty list!";
return first->getData();
}
template <typename T>
T LinkedList<T>::getLast() {
if(n < 1)
throw "Can't retrieve element from empty list!";
return last->getData();
}
template <typename T>
Node<T>* LinkedList<T>::getFirstNode() const {
return first;
}
template <typename T>
int LinkedList<T>::size() {
return n;
}
template <typename T>
T LinkedList<T>::getAt(int pos) {
if(pos >= n)
throw "Element index out of bounds!";
Node *temp = first;
while(pos > 0) {
temp = temp->next;
pos--;
}
return temp->getData();
}
template <typename T>
void LinkedList<T>::clear() {
Node *current = first;
while(current) {
Node *next = current->next;
delete current;
if(next)
current = next;
else
current = 0;
}
}
template <typename T>
void LinkedList<T>::addAt(int pos, T data) {
if(pos >= n)
throw "Element index out of bounds!";
if(pos == 0)
addFirst(data);
else {
Node *temp = first;
while(pos > 1) {
temp = temp->next;
pos--;
}
Node *p = new Node(data, temp->next);
temp-> next = p;
n++;
}
}
template <typename T>
T LinkedList<T>::removeAt(int pos) {
if(pos >= n)
throw "Element index out of bounds!";
if(pos == 0)
return removeFirst();
if(pos == n - 1)
return removeLast();
else {
Node *p = first;
while(pos > 1) {
p = p->next;
pos--;
}
T a = p->next->getData();
Node *temp = p->next;
p->next = p->next->next;
delete temp;
n--;
return a;
}
}
template <typename T>
LinkedList<T>::~LinkedList() {
clear();
}
template <typename T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T> &right) {
if(this != &right) {
n = 0;
first = 0;
last = 0;
Node *temp = right.first;
while(temp) {
addLast(temp->getData());
temp = temp->getNext();
}
}
return *this;
}
template <typename T>
T LinkedList<T>::operator[](int i) {
return getAt(i);
}
template <typename T>
LinkedList<T>& LinkedList<T>::operator+(const LinkedList<T> &right) {
Node *temp = right.first;
while(temp) {
addLast(temp->getData());
temp = temp->getNext();
}
return *this;
}
template <typename T>
LinkedList<T>& LinkedList<T>::operator+=(const LinkedList<T> &right) {
Node *temp = right.first;
while(temp) {
addLast(temp->getData());
temp = temp->getNext();
}
return *this;
}
template <typename T>
std::ostream& operator<<(std::ostream &os, const LinkedList<T> &ll) {
Node *temp = ll.getFirstNode();
while(temp) {
os<<temp->getData()<<std::endl;
temp = temp->getNext();
}
return os;
}
#endif
Node.h
#ifndef NODE_H
#define NODE_H
template <typename T>
class Node {
private:
T data;
public:
Node<T>* next;
T getData();
Node<T>* getNext();
Node(T data, Node<T>* next);
Node(const Node & n);
};
template <typename T>
T Node<T>::getData() {
return data;
}
template <typename T>
Node<T>* Node<T>::getNext() {
return next;
}
template <typename T>
Node<T>::Node(T data, Node<T>* next) {
this->data = data;
this->next = next;
}
template <typename T>
Node<T>::Node(const Node & n) {
data = n.data;
next = n.next;
}
#endif
List.h
#ifndef LIST_H
#define LIST_H
class List
{
public:
virtual void addFirst(int data) = 0;
virtual void addAt(int pos, int data) = 0;
virtual void addLast(int data) = 0;
virtual int getFirst()= 0;
virtual int getAt(int pos) = 0;
virtual int getLast()= 0;
virtual int removeFirst()= 0;
virtual int removeAt(int pos) = 0;
virtual int removeLast()= 0;
virtual int size() = 0;
virtual void clear() = 0;
virtual ~List() {};
};
#endif
For this, I get LNK2019 and LNK1120 linking errors. I know I used to get this when implementing a Queue in separated .h and .cpp files. But worked around it by doing everything in the header. I also know that this can happen when not implementing a named method, but I can't find any of those here. So what's causing this? I wish the compiler / IDE could point me to the possible cause of the error. But then again, if it was an easy task to find the faulty line, I assume it would already do this. VS 2012 btw.
You made main a function template. Not only does this not make sense (there is no mention of the template parameter inside), it's also never instantiated (and even if it was, it probably wouldn't resolve to the correct main that a program needs as a start point).
Furthermore, it should be int main rather than void main.
// template <typename T>
void main() {
LinkedList<int> a;
a.addFirst(22);
a.addFirst(24);
a.addFirst(28);
LinkedList<int> b;
b = a;
b = b + a;
b += a;
cout<<b;
}
You need a main function, not a main function template. That's probably the source of your linker error: no function called "main".
The reason this won't work is primarily because class templates and function templates are never expanded to real code unless they're used. Since main is the entrypoint to your program, you never call main from anywhere and thus no code for main is ever generated.
Furthermore due to the name mangling that C++ compilers do to functions (to handle overloading, templates, namespaces etc) the symbol that will be generated in the resulting assembly for this main template probably won't be the right one. If it's looking for a symbol 'main' and it sees
$__T_float_main_blah_blah_blah
then you won't link anyways. Long story short: main is a function, not a function template.