Hi.
I'm learning by myself data structures with c++ but i got stuck
Here's my code:
#include<iostream>
#include<cstring>
#include<string>
#include<sstream>
using namespace std;
template<class T>
// node class
class nodeType
{
public:
nodeType();
nodeType(T);
~nodeType();
nodeType *next;
nodeType *previous;
T data;
//string getkey();
string Id;
//void borra_todo();
void imprime();
// getkey
string getkey()
{
stringstream llave;
llave << data.getkey();
llave >> Id;
return Id;
}
};
// by defect
template<typename T>
nodeType<T>::nodeType()
{
data = NULL;
next = NULL;
previous = NULL;
}
// by parameter
template<typename T>
nodeType<T>::nodeType(T data_)
{
data = data_;
next = NULL;
previous = NULL;
}
// borra todo los nodos ...
// template<typename T>
// void node<T>::borra_todo()
// {
// if (next)
// next->borra_todo();
//
// delete this;
// }
// node printing
template<typename T>
void nodeType<T>::imprime()
{
cout << data << endl;
// data.Print();
}
template<typename T>
nodeType<T>::~nodeType()
{
delete next;
delete previous;
}
// list
template <class T>
class list
{
private:
nodeType<T> *first;
nodeType<T> *last;
nodeType<T> *current;
int m_num_nodes;
public:
list();
~list();
void add_head(T);
void add_end(T);
void add_sort(T);
//void fill();
void search(T);
void del_by_data(T);
void print();
};
template<typename T>
list<T>::list()
{
m_num_nodes = 0;
first = NULL;
last = NULL;
}
//add in the beginning ...
template<typename T>
void list<T>::add_head(T data_)
{
nodeType<T> *new_node = new nodeType<T>(data_);
nodeType<T> *temp = first;
if (!first)
{
first = new_node;
}
else
{
new_node->next = first;
first = new_node;
while (temp)
{
temp = temp->next;
}
}
m_num_nodes++;
}
// add to the last
template<typename T>
void list<T>::add_end(T data_)
{
nodeType<T> *new_node = new nodeType<T> (data_);
nodeType<T> *temp = first;
if (!first)
{
first = new_node;
}
else
{
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = new_node;
}
m_num_nodes++;
}
// it is supposed that sorts items in the list ...
template<typename T>
void list<T>::add_sort(T data_)
{
nodeType<T> *new_node = new nodeType<T> (data_);
nodeType<T> *temp = first;
if (!first)
{
first = new_node;
last = new_node;
}
else
{
if (first->getkey() > new_node->getkey())
{
new_node->next = first;
first = new_node;
}
else
{
while ((temp->next != NULL) && (new_node->getkey() > temp->next->getkey()))
{
temp = temp->next;
}
// .. ¿? ..
new_node->next = temp->next;
temp->next = new_node;
temp->previous = new_node->previous;
new_node->previous = temp;
temp->next = new_node;
}
}
m_num_nodes++;
}
// ... llena elementos en la lista ...
//template<typename T>
//void list<T>::fill(char rfc[30], char nombre[30])
// void list<T>::fill()
// {
// //char temprfc[30];
// //char tempnombre[30];
// string temprfc;
// string tempnombre;
//
// temprfc = "DUDE010101R0";
// tempnombre = "Dude";
// //for (int i = 0;i <= 1;i++)
// add_sort(temprfc, tempnombre);
// temprfc = "AUDE010101R1";
// tempnombre = "Commander";
// add_sort(temprfc, tempnombre);
// }
// print list
template<typename T>
void list<T>::print()
{
nodeType<T> *current;
current = first;
while (current != NULL)
{
current->imprime();
current = current->next;
}
}
// search the list
template<typename T>
void list<T>::search(T data_)
{
nodeType<T> *temp = first;
int cont=1;
int cont2=0;
while(temp)
{
if(temp->data,data_)
{
cout<<"Element found " << temp->data;
cout << " in position: " << cont << endl;
cont2++;
}
temp=temp->next;
cont++;
}
if(cont2==0)
{
cout << "Element not found"<<endl;
}
}
// ... delete by data ... ¿?
template<typename T>
void list<T>::del_by_data(T data_)
{
nodeType<T> *temp = first;
nodeType<T> *temp1 =first->next;
int cont = 0;
if (first->data == data_)
{
first = temp->next;
}
else
{
while (temp1)
{
if (temp1->data == data_)
{
nodeType<T> *aux_node = temp1;
temp->next = temp1->next;
delete aux_node;
cont++;
m_num_nodes--;
}
temp = temp->next;
temp1 = temp1->next;
}
}
if (cont == 0)
{
cout << "No data" << endl;
}
}
// destroy the constructor
template<typename T>
list<T>::~list() {}
class Empleado
{
private:
string RFC;
string Nombre;
public:
Empleado();
Empleado(string r, string nom);
~Empleado();
void imprime();
string getkey()
{
return RFC;
}
};
Empleado::Empleado()
{
RFC = "";
Nombre = "";
}
Empleado::Empleado(string r, string nom)
{
RFC = r;
Nombre = nom;
}
void Empleado::imprime()
{
cout << "RFC: " << RFC << endl;
cout << "Nombre: " << Nombre << endl;
}
// -------
int main()
{
list<Empleado> * list1 = new list<Empleado>();
string element1;
string element2;
int dim, choice, pos;
do{
cout << "Select a choice.\n";
cout << "1. Print list\n";
cout << "2. Delete an element of the list\n";
cout << "3. Search an element of the list\n";
cout << "4. Exit\n";
cin >> choice;
switch(choice)
{
case 1:
{
cout << "Printing list:\n";
//list1->fill();
list1->add_sort(Empleado("DUDE010101R0", "Juan Peres"));
list1->add_sort(Empleado("AUDE010101R1", "Ana Banana"));
list1->print();
}
case 2:
{
cout << "Element to delete: DUDE010101R0";
//cin >> element1;
element1 = "DUDE010101R0";
//list1->search(element1);
list1->search("DUDE010101R0");
element1 = "";
break;
}
case 3:
{
cout << "Element to search: AUDE010101R1";
//cin >> element1;
//list1->search(&element1);
list1->search("AUDE010101R1");
element1 = "";
break;
}
}
}while(choice != 4);
return 0;
}
I got the errors:
t3b.cpp: In function ‘int main()’:
t3b.cpp:369:30: error: no matching function for call to ‘list<Empleado>::search(const char [13])’
list1->search("DUDE010101R0");
t3b.cpp:369:30: note: candidate is:
t3b.cpp:232:6: note: void list<T>::search(T) [with T = Empleado]
void list<T>::search(T data_)
Any idea how to fix this? or what i'm doing wrong?
Thanks in advance