segmentation fault C++ - c++

I don't know why my code is seg faulting, im assuming im looking at a point in the queue where theres nothing there, but I thought that I do push elements onto the queue.
heres my code:
template <typename T>
class btree {
public:
btree(size_t maxNodeElems);
~btree() {}
struct node {
list <T> elements;
node *lvl;
};
private:
size_t maxNodeElems;
node* root;
};
template <typename T>
btree<T>::btree(size_t maxNodeElems) {
if (maxNodeElems > 0) max = maxNodeElems;
root = new node;
root->lvl = new node*[max+1];
for (int i = 0; i < (int) max+1; i++) root->lvl[i] = new node;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const btree<T>& tree) {
queue <typename btree<T>::node*> q;
q.push(tree.root);
int loop = 0;
while (!q.empty()) {
loop++;
typename btree<T>::node* temp = q.front();
int i = 0;
class list <T>::iterator itr = temp->elements.begin();
for (; itr != temp->elements.end(); ++itr) {
os << *itr << " ";
if (!temp->lvl[i]->elements.empty()) {
q.push(temp->lvl[i]);
}
i++;
}
q.pop();
}
return os;
}
could someone help me out, im lost because when I check if the if statement is working inside the for loop, it does go into it, but im not sure if its pushing
EDIT: more code
template <typename T>
pair <typename btree<T>::iterator, bool> btree <T>::insert (const T& elem) {
pair <typename btree<T>::node, bool> start;
start = addElement (elem, root);
pair <typename btree<T>::iterator, bool> final;
return final;
}
template <typename T>
pair <typename btree<T>::node, bool> btree<T>::addElement (const T& e, typename btree<T>::node*& n) {
pair <typename btree<T>::node, bool> elemPair;
if (n->elements.size() == max) {
int count = 0;
class list <T>::iterator itr = n->elements.begin();
for (; itr != n->elements.end(); ++itr) {
count++;
if (e < *itr) {
count--;
elemPair = addElement (e, n->lvl[count]);
} else if (e == *itr) return make_pair (*n, false);
}
} else {
n->elements.push_back(e);
n->elements.sort();
}
return make_pair(*n, true);
}
in my test.cpp:
int main (void) {
btree<char> b(2);
b.insert('Z'); b.insert('J'); b.insert('Y');
cout << b;
return 0;
}

The code you post doesn't compile and so I can't verify your error.
The following does compile and so might provide some help. Its similar to the code you provided.
However, the following is not good code - for example, you need to be vary careful about using the new keyword in a constructor, because exception handing in a constructor is more difficult (and there is the chance of an allocation exception being thrown).
template <typename T>
class btree {
public:
btree(size_t maxNodeElems);
//if your using new then you need to delete - do that in the destructor.
~btree();
struct node {
list <T> elements;
//you use this as a pointer to an array,
// it can be demoted to a double pointer -
// but not a single pointer as you had.
node ** lvl;
};
private:
size_t maxNodeElems; //you don't actually use this variable in your code
node* root;
//You are doing memory allocations in the constructor,
// therefore need to overload operator= and copy constructor.
// Since we are not overloading these, we make them private.
void operator=(const btree&);
btree(const btree&);
};
//In your code you had a undeclared variable max.
// No idea what it was or why you had it - removed.
template <typename T>
btree<T>::btree(size_t v_maxNodeElems)
: maxNodeElems(v_maxNodeElems) // construct the variable you define
{
root = new node;
root->lvl = new node*[maxNodeElems+1]; //root->lvl is a double pointer
for (int i = 0; i < (int) maxNodeElems+1; i++)
root->lvl[i] = new node;
}
//delete in reverse order to you constructing
template <typename T>
btree<T>::~btree() {
for (int i = 0; i < (int) maxNodeElems+1; i++)
delete root->lvl[i]; //first the elements of the array
delete[] root->lvl; //then the array - note the delete[]
delete root; //then the root.
}
int
main (int ac, char **av)
{
btree<char> b(2);
}

Related

How to prevent memory leak in generalized linked list?

I've implemented my own linked list data structure. Data is stored inside Node struct. Code is as follows
// NODE
template <typename T>
struct Node
{
T data;
Node<T> *next;
Node(T);
};
template <typename T>
Node<T>::Node(T d) : data(d), next(NULL) {}
// LIST
#include "node.cpp"
template <typename T>
class List
{
Node<T> *head;
int size;
public:
List(); // Default constructor
List(const List &); // Copy constructor
void push_back(const T &); // Insert element to the end of the list
int get_size() const; // Get the current size of the list
T &operator[](int) const; // Overload [] operator
void operator=(const List &); // Overload = operator
~List(); // Destructor
};
template <typename T>
List<T>::List() : head(NULL), size(0) {}
template <typename T>
List<T>::List(const List &list) : head(NULL), size(0)
{
for (int i = 0; i < list.size; i++)
push_back(list[i]);
}
template <typename T>
void List<T>::push_back(const T &data)
{
// Create new Node with data
Node<T> *nn = new Node<T>(data);
// Find insert position
if (head == NULL)
{
head = nn;
size++;
return;
}
Node<T> *traverse = head;
while (traverse->next)
traverse = traverse->next;
// Traverse points to end of the list
traverse->next = nn;
size++;
}
template <typename T>
int List<T>::get_size() const
{
return size;
}
template <typename T>
T &List<T>::operator[](int index) const
{
int count = 0;
Node<T> *traverse = head;
while (traverse && count < index)
{
traverse = traverse->next;
count++;
}
return traverse->data;
}
template <typename T>
void List<T>::operator=(const List<T> &list)
{
Node<T> *traverse = head;
while (head)
{
traverse = head;
head = head->next;
delete traverse;
}
size = 0;
for (int i = 0; i < list.getSize(); i++)
push_back(list[i]);
}
template <typename T>
List<T>::~List()
{
Node<T> *traverse = head;
while (head)
{
traverse = head;
head = head->next;
delete traverse;
}
}
Problem is with memory leak. Consider the following main file
#include "list.cpp"
using namespace std;
List<int *> l;
void func()
{
int *i = new int[2];
i[0] = 1;
i[1] = 2;
l.push_back(i);
}
int main()
{
func();
return 0;
}
This program has memory leak according to Valgrind. It is because that Node does not have a destructor so it can not delete data inside it. However, I can not add a destructor to Node because, suppose that I am using List<int> so it is an error to delete something that was not dynamically allocated. In short, whenever I use a dynamically allocated data type for List, I get memory leak. How can I overcome this situation? Thanks.
The leak in your example has nothing to with the list. You leak the same with:
void func()
{
int *i = new int[2];
i[0] = 1;
i[1] = 2;
}
You have to delete what you created via new and delete[] what you created via new[]. To fix the leak:
void func()
{
int *i = new int[2];
i[0] = 1;
i[1] = 2;
l.push_back(i);
delete [] i;
}
However, note that then after the delete[] you have a dangling pointer in the list.
It is not the Lists buisness to delete objects when you push raw pointers to it. The list cannot know if those are owning pointers or not. For example:
void func()
{
int i = 0;
l.push_back(&i);
}
No need to delete anything here. (Though, same here: once the function returns you have a dangling pointer in the list)
Neither of the abvove is really "ok". Don't use raw owning pointers! Use smart pointers instead. And if you want a list of integers then use a List<int> (or rather a std::list<int>).
Use std::unique_ptr as the data type for the nodes, eg:
List<std::unique_ptr<int[]>> l;
When each node is destroyed, its destructor will destroy its unique_ptr data, which will in turn call delete[] on the int* pointer it is holding.

Reversing Linked List with Recursion, using STL

Code for Reversing Linked List with Recursion, using STL
#include<iostream>
#include<conio.h>
#include<list>
using namespace std;
template<typename T>
class node
{
public:
T data;
node<T> *next;
node(){ next = NULL; }
node(const T& item, node<T> *nextnode = NULL)
{
data = item;
next = nextnode;
}
};
template<typename T>
class Reverse_list
{
private:
node<T> *head;
void reverse(node<T> *front);
public:
Reverse_list(){ head = NULL; }
//template<typename T>
void Reverse();
template<typename T>
void Display( list<T>& alist );
};
int main()
{
Reverse_list <int> rl;
list<int> intlist;
int size, no;
cout << "Size of List ?? ";
cin >> size;
for (int i = 1; i <= size; i++)
{
cout << "Enter the " << i <<" "<< "element";
cin >> no;
intlist.push_front(no);
}
rl.Display(intlist);
rl.Reverse();
rl.Display(intlist);
_getch();
return 0;
}
template<typename T>
void Reverse_list<T>::Display(list<T>& alist)
{
list<int>::iterator iter = alist.begin();
while (iter != alist.end())
{
cout << *iter << " ";
iter++;
}
}
template<typename T>
void Reverse_list<T>::reverse(node<T> *front)
{
if (front->next == NULL)
{
head = front;
return;
}
reverse(front->next);
node<int> *back = front->next;
back->next = front;
front->next = NULL;
}
template<typename T>
void Reverse_list<T>::Reverse()
{
reverse(head);
}
The above code generates 2 errors.
Error 1) No instance of function template matches the argument list. ( No error number.)
If I remove line 1 ( mentioned in a code ) then above error is no more. ( Why? )
Error 2) C2783: 'void Reverse_list::Reverse1(void)' : could not deduce template argument for 'T'
How to solve above errors.
In above program , I wanted to pass " head" node ( which is private ) as
argument to Reverse function. But we can not access private member outside of the class. So I passed indirectly. Is this a correct way of passing ?? Or there is some other way of accessing private data ??
I'm not sure to understand your intentions but...
You're trying to declare a method (reverse()) inside another method (Reverse()) ? Uhmmm....
We return to this later.
Imagine that the following instruction is correct instruction of Reverse_list<T>::Reverse()
node<T> *back = front->next;
Why you declare back as a pointer to a generic Node<T> when you assign front->next (so a specific Node<int>) to it?
If you define back as a node<int> pointer, the method Reverse() has no longer reason to be a template (dependant from T) method. And you can avoid both errors.
With your actual code, when you call
rl.Reverse();
you call a template method but the compiler doesn't know how to determine the type T. You could explicit it in this way
rl.Reverse<int>();
but, as written before, I thik it's better if you remove the whole template part.
Or, alternatively, you can transform the whole class in a template class; where head is a pointer to a generic Node<T>, not a specifica Node<int>.
Something like (if I understand correctly your intentions)
template <typename T>
class Reverse_list
{
private:
node<T> *head;
void reverse (node<T> * front);
public:
Reverse_list() : head(NULL)
{ }
void Reverse();
void Display(list<T>& alist);
};
template<typename T>
void Reverse_list<T>::reverse (node<T> * front)
{
if (front->next == NULL)
{
head = front;
return;
}
reverse(front->next);
node<T> *back = front->next;
back->next = front;
front->next = NULL;
}
template<typename T>
void Reverse_list<T>::Reverse()
{ reverse(head); }
In this case, in main(), rl should be declared as
Reverse_list<int> rl;
fixing T as int, and the call to Reverse() should be
rl.Reverse();
--- EDIT 2016.05.10 ---
With the "template Reverse_list" solution, you should correct three points (at last).
1) in Reverse_list class declaration, you have commented the template<typename T> row before void Reverse(); good; you should delete (comment) the same line (for the same reason) before void Display( list<T>& alist );; so the class become
template<typename T>
class Reverse_list
{
private:
node<T> *head;
void reverse(node<T> *front);
public:
Reverse_list(){ head = NULL; }
//template<typename T>
void Reverse();
//template<typename T>
void Display( list<T>& alist );
};
2) Display() now is a method of a templated class; so the line
list<int>::iterator iter = alist.begin();
become
list<T>::iterator iter = alist.begin();
3) reverse() now is a method of a templated class; so the line
node<int> *back = front->next;
become
node<T> *back = front->next;

C++ Segmentation Fault: Passing a string to a node in a linked list

I'm new to working with class templates and am simply trying to define a temporary node 'temp' in a class associated with the Linked List, which sets the string that the node stores to some temporary string that is created in the function TowerHanoi::set_Discs(size_t disc) via user input. When I call the function temp->set_data(tmp_str) i get a segmentation fault. I tried calling temp->set_data("hello"); on its own and i still get the error.
I'm not sure what's going on here and i've tried researching into it but to no avail. I'm probably missing something obvious, but i'm just quite lost now. Let me know if you need more code. Thanks.
TowerHanoi.cpp:
#include "TowerHanoi.h"
#include <iostream>
#include <cstdlib>
using namespace std;
using oreilly_A2::node;
namespace oreilly_A2 {
TowerHanoi::TowerHanoi() {
for (int i=0;i<2;i++) {
rod[i] = LStack<node<std::string> >();
}
}
TowerHanoi::TowerHanoi(size_t numDiscs) {
for (int i=0; i < 2; i++) {
rod[i] = LStack<node<string> >();
}
discs = numDiscs;
}
void TowerHanoi::set_Discs(size_t disc) {
node<string>* temp=NULL;
while (disc != 0) {
string tmp_str;
for (size_t i=0; i<disc; i++) {
tmp_str.append("x");
}
disc--;
temp->set_data(tmp_str);
rod[0].push(temp);
}
void TowerHanoi::print_Game() {
for (size_t s=1; s<discs; s++) {
cout << " ";
for (size_t o=1; o<discs-s;o++) {
cout << " ";
}
//cout << tmp_node->data() << endl;
cout << "x" << endl;
}
}
}
node.h file:
#ifndef NODE_CAMERON_H
#define NODE_CAMERON_H
#include <string>
namespace oreilly_A2 {
template <typename Item>
class node {
public:
node(); //constructor for node
node(const Item val, node* newNext); //constructor with parameters
~node(); //destructor
void set_data(Item new_data); //set the word that this node contains
void set_link(node* new_link); //set the 'next' node
void set_previous(node* new_prev);
Item data() const; //return this node's word
const node* link() const; //return next
const node* back() const;
node* link(); //return next
node* back();
private:
node* next; //the next node
node* previous;
Item word; //the word this node contains
};
}
#include "Node.template"
#endif
node.template file:
namespace oreilly_A2 {
template <typename Item>
node<Item>::node() {
next=NULL;
previous=NULL;
}
//Node.template
template <typename Item>
node<Item>::node(const Item val, node* newNext=NULL) {
word = val;
next = newNext;
}
template <typename Item>
node<Item>::~node() {
delete next;
delete previous;
delete word;
}
template <typename Item>
void node<Item>::set_data(Item new_data){
word = new_data;
}
template <typename Item>
void node<Item>::set_link(node* new_link){
next = new_link;
}
template <typename Item>
void node<Item>::set_previous(node* new_back) {
previous = new_back;
}
template <typename Item>
Item node<Item>::data() const { //return the word
return word;
}
template <typename Item>
const node<Item>* node<Item>::link() const { //return next node (const function)
return next;
}
template <typename Item>
const node<Item>* node<Item>::back() const { //return previous node (const)
return previous;
}
template <typename Item>
node<Item>* node<Item>::link() {
return next; //return next node (non-const)
}
template <typename Item>
node<Item>* node<Item>::back() { //return previous node (const)
return previous;
}
}
Unless I have missed something the temp variable is NULL at the time of calling set_data. As any regular object you need to first initialized it.
node<string>* temp=new node<string>();
And then freeing it when appropriate to avoid memory leaks.
This is not the case with temp_str because the later is not a pointer, it's a value so it gets initialized automatically (and also freed automatically when it gets out of scope).
You have initialized temp as NULL. So when you are trying to do temp->set_data(tmp_str); you are actually trying to access NULL pointers.
All you need to do is initialize temp. I have correct the code below
void TowerHanoi::set_Discs(size_t disc) {
node<string>* temp=new node<string>();
while (disc != 0) {
string tmp_str;
for (size_t i=0; i<disc; i++) {
tmp_str.append("x");
}
disc--;
temp->set_data(tmp_str);
rod[0].push(temp);
}
To avoid memory leak you need to delete all the memory allocated after you are done.

c2955 Error - Use of Class template reuires argument list

So, I've tested vector and it seems to be running fine. However, I'm trying to implement a basic Stack class built off of my Vector class. I keep running into these errors when I go to build:
stack.h(4): error C2955: 'Vector' : use of class template requires template argument list
followed by:
vector.h(11) : see declaration of 'Vector'
stack.h(13) : see reference to class template instantiation 'Stack<T>' being compiled
Here is the Vector.h file:
#include<iostream>
using namespace std;
const int SIZEFACTOR = 4;
template <class T>
class Vector{
private:
unsigned int size_Of_Vector; // # of Items in list
unsigned int total_Vector_Capacity;//Total Capacity
T * vector_array;//Items themselves
public:
Vector();
~Vector();
void push_back(const T &e);
void pop_back();
bool empty();
int size() const;
void growVector();
void shrinkVector();
void shrinkToSize();
int currentCapacity() const;
//Operator
const T & operator [] (int index){
if((index >= size_Of_Vector) || index < 0){
cout << "ERROR! Index not used: " << index
<< " (max = " << size_Of_Vector << ")" << endl;
return EXIT_FAILURE;
}
return vector_array[index];
};//End Operator
};//End Header Definition
template <class T>
Vector<T>::Vector(){//Constructor
total_Vector_Capacity = 2;//Initially two items
size_Of_Vector = 0;//Nothing is in the vector yet
vector_array = new T [total_Vector_Capacity];//Initially two items
}
template <class T>
Vector<T>::~Vector (){//Destructor
total_Vector_Capacity = 0;
size_Of_Vector = 0;
delete [] vector_array;
}
template <class T>
void Vector<T>::growVector(){
total_Vector_Capacity = total_Vector_Capacity * SIZEFACTOR; //Quarter Size
//Temp Array
T * temp_array;
temp_array = new T [total_Vector_Capacity];
//Copy
for(unsigned int i = 0; i < size_Of_Vector; i++){
temp_array[i] = vector_array[i];
}
//Delete old array
delete [] vector_array;
//Re-initilize main array
vector_array = new T [total_Vector_Capacity];
//Copy old Data back to original array
for(unsigned int i = 0; i < size_Of_Vector; i++){
vector_array[i] = temp_array[i];
}
//Delete temp array
delete [] temp_array;
}
template <class T>
void Vector<T>::shrinkVector(){
total_Vector_Capacity = (int) (total_Vector_Capacity / SIZEFACTOR); //Quarter Size
//Temp Array
T * temp_array;
temp_array = new T [total_Vector_Capacity];
//Copy
for(unsigned int i = 0; i < size_Of_Vector; i++){
temp_array[i] = vector_array[i];
}
//Delete old array
delete [] vector_array;
//Re-initilize main array
vector_array = new T [total_Vector_Capacity];
//Copy old Data back to original array
for(unsigned int i = 0; i < size_Of_Vector; i++){
vector_array[i] = temp_array[i];
}
//Delete temp array
delete [] temp_array;
}
template <class T>
void Vector<T>::shrinkToSize(){
total_Vector_Capacity = size_Of_Vector; //Quarter Size
//Temp Array
T * temp_array;
temp_array = new T [total_Vector_Capacity];
//Copy
for(unsigned int i = 0; i < size_Of_Vector; i++){
temp_array[i] = vector_array[i];
}
//Delete old array
delete [] vector_array;
//Re-initilize main array
vector_array = new T [total_Vector_Capacity];
//Copy old Data back to original array
for(unsigned int i = 0; i < size_Of_Vector; i++){
vector_array[i] = temp_array[i];
}
//Delete temp array
delete [] temp_array;
}
template <class T>
void Vector<T>::push_back(const T &e){
if(size_Of_Vector == total_Vector_Capacity){//Resize if size equals capacity
cout << "\nGrow now\n";
growVector();
}
vector_array[size_Of_Vector]=e;
size_Of_Vector++;//Increase Vector Size
}
template <class T>
void Vector<T>::pop_back(){
if(size_Of_Vector == (int)(total_Vector_Capacity/SIZEFACTOR)){//Resize if size equals capacity
cout << "\nShrink now\n";
shrinkVector();
}
size_Of_Vector--;//Increase Vector Size
}
template <class T>
bool Vector<T>::empty(){
if(size_Of_Vector==0){
return true;
}
else{
return false;
}
}
template <class T>
int Vector<T>::size() const{
return size_Of_Vector;
}
template <class T>
int Vector<T>::currentCapacity() const{
return total_Vector_Capacity;
}
and stack.h:
template <class T>
class Stack : public Vector{
private:
Vector<T> stack;
public:
Stack(){};
void push(T x) {stack.push_back(&x)};
void pop(){stack.pop_back()};
bool empty(){stack.empty()};
};
You have defined Vector<T> as a member of Stack already, it's not necessary to inherit from Vector
Update
template <class T>
class Stack : public Vector{
//...
};
To:
template <class T>
class Stack {
Vector<T> stack;
//...
};
Or if you want to inherit from a template class, you need to provide template parameter
template <class T>
class Stack : public Vector<T>{
// ^^^
Note:
You miss a few semicolons in Stack function definition and a bug in Stack::push_back as well, I may suggest update to below:
template <class T>
class Stack
{
private:
Vector<T> stack;
public:
Stack(){};
void push(T const& x) {stack.push_back(x);}
void pop(){stack.pop_back();}
bool empty(){return stack.empty();}
};

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.