G
Gaijinco
I'm doing a template class to make an ordered list.
I created a class Person to make an agenda.
When I create a new person, there's no problem but when I try to add
it to the list it throws a lot of errors.
Can someboy help me! Thanks!
#include <iostream>
#include <string>
template <typename T>
class List
{
class Node
{
T data;
Node* next;
public:
Node(T data)
{
this->data = data;
next = (Node*)NULL;
}
T getData() const
{
return data;
}
Node* getNext() const
{
return next;
}
void setData(T data)
{
this->data = data;
return;
}
void setNext(Node* next)
{
this->next = next;
return;
}
};
Node* head;
size_t nodes;
public:
List()
{
head = (Node*)NULL;
nodes = 0;
}
void insert(T data)
{
Node* p = new Node(data);
if(p!=(Node*)NULL)
{
if(nodes==0)
head = p;
else if(head->data > data)
p->setNext(head);
else
{
bool done=false;
Node* q = head;
while(head!=(Node*)NULL && !done)
if(head->getNext()->getData() < data)
done = true;
else
head = head->getNext();
if(done)
q->setNext(p);
else
p->setNext(q);
}
++nodes;
}
}
size_t size() const
{
return nodes;
}
};
class Person
{
std::string name;
std::string lastname;
std::string telephone;
public:
Person(std::string n, std::string l, std::string
t):name(n),lastname(l),telephone(t){};
std::string getName() const { return name; }
std::string getLastname() const { return lastname; }
std::string getTelephone() const { return telephone; }
void setName(std::string name) { this->name = name;
return; }
void setLastname(std::string name) { this->lastname =
lastname; return; }
void setTelephone(std::string name) { this->telephone =
telephone; return; }
friend bool operator<(Person,Person);
friend bool operator>(Person,Person);
};
bool operator<(Person a, Person b)
{
return a.lastname < b.lastname;
}
bool operator>(Person a, Person b)
{
return a.lastname > b.lastname;
}
int main()
{
List<Person> agenda;
Person p("John","Doe","555-123456");
agenda.insert(p);
return 0;
}
I created a class Person to make an agenda.
When I create a new person, there's no problem but when I try to add
it to the list it throws a lot of errors.
Can someboy help me! Thanks!
#include <iostream>
#include <string>
template <typename T>
class List
{
class Node
{
T data;
Node* next;
public:
Node(T data)
{
this->data = data;
next = (Node*)NULL;
}
T getData() const
{
return data;
}
Node* getNext() const
{
return next;
}
void setData(T data)
{
this->data = data;
return;
}
void setNext(Node* next)
{
this->next = next;
return;
}
};
Node* head;
size_t nodes;
public:
List()
{
head = (Node*)NULL;
nodes = 0;
}
void insert(T data)
{
Node* p = new Node(data);
if(p!=(Node*)NULL)
{
if(nodes==0)
head = p;
else if(head->data > data)
p->setNext(head);
else
{
bool done=false;
Node* q = head;
while(head!=(Node*)NULL && !done)
if(head->getNext()->getData() < data)
done = true;
else
head = head->getNext();
if(done)
q->setNext(p);
else
p->setNext(q);
}
++nodes;
}
}
size_t size() const
{
return nodes;
}
};
class Person
{
std::string name;
std::string lastname;
std::string telephone;
public:
Person(std::string n, std::string l, std::string
t):name(n),lastname(l),telephone(t){};
std::string getName() const { return name; }
std::string getLastname() const { return lastname; }
std::string getTelephone() const { return telephone; }
void setName(std::string name) { this->name = name;
return; }
void setLastname(std::string name) { this->lastname =
lastname; return; }
void setTelephone(std::string name) { this->telephone =
telephone; return; }
friend bool operator<(Person,Person);
friend bool operator>(Person,Person);
};
bool operator<(Person a, Person b)
{
return a.lastname < b.lastname;
}
bool operator>(Person a, Person b)
{
return a.lastname > b.lastname;
}
int main()
{
List<Person> agenda;
Person p("John","Doe","555-123456");
agenda.insert(p);
return 0;
}