Sum up price element in node of linked list
Sep 6, 2014 at 3:00pm UTC
Hi guys,
I'm trying to calculate the total price of books contained in a linked list. I've tried
getTotal()
function in linked list but
error: no match for 'operator+=' in 'total += ptr->Node<Book>::info'|
occurred. How can I access the price in the node and then calculate the total price.
Here is part of my code :
Book class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
class Book {
protected :
int id;
string title;
double price;
string bookStatus;
public :
Book (int id = 0, string title = "NULL" , double price = 0.00, string bookStatus = "NULL" )
: id(id), title(title), price(price), bookStatus(bookStatus) {}
int getID() {return id;}
string getTitle() {return title;}
double getPrice() {return price;}
friend ostream& operator << (ostream& os, Book& p) {
os << "[" << p.id << ", " << p.title << ", RM" << p.price << ", " << p.bookStatus << "]" ;
return os;
}
// friend Book operator+ (Book t1, Book t2) {
// double totalSales = 0;
// t1 = totalSales;
// totalSales = t1.price + t2.price;
// return Book(totalSales);
// }
};
Linked List class :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
template <typename T>
struct Node {
T info;
Node<T> *next;
};
template <typename T>
class LinkedList {
Node<T> *start;
public :
LinkedList() { start = NULL; }
~LinkedList() {
while (start != NULL){
Node <T> *ptr =start;
start = start -> next;
delete ptr;
}
}
void insertFront (T& newElement) {
Node<T>* newNode = new Node<T>;
newNode->info = newElement;
newNode->next = start;
start = newNode;
}
double getTotal()
{
Node<T>* ptr = start;
double total = 0;
while (ptr != NULL) // or simply while (currentPtr)
{
total += ptr -> info;
ptr = ptr -> next;
}
return total;
}
friend ostream& operator << (ostream& os, LinkedList<T>& list) {
// first item has index 0.
os << "Book : \n" ;
Node<T> *ptr = list.start;
int i = 0;
while (ptr != NULL) {
os << i++ << " " << ptr->info << endl;
ptr = ptr->next;
}
return os;
}
};
Main function :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
int main() {
LinkedList<Book> list;
Book newBook;
double total = 0;
newBook = Book(111, "EPL" , 59.00);
list.insertFront(newBook);
newBook = Book(222, "Primera" , 77.30);
list.insertFront(newBook);
newBook = Book(333, "Seria A" , 25.95);
list.insertFront(newBook);
newBook = Book(444, "Eredivisie" , 1.50);
list.insertFront(newBook);
cout << list << endl;
total = total + list.getTotal();
cout << "\n\n" << total;
}
Sep 6, 2014 at 3:26pm UTC
The problem with
total += ptr -> info;
is that the
info
is a
Book
.
This fails for same reason:
1 2 3
Book newBook;
double total = 0;
total += newBook;
How do you access price of a Book?
1 2 3
Book newBook;
double total = 0;
total += newBook.getPrice();
Sep 6, 2014 at 3:51pm UTC
1 2 3
Book newBook;
double total = 0;
total += newBook.getPrice();
I've changed my main function with your above code and it ran no error but it only displayed the price of the last book entered, didn't sum up all price of books entered. Should I modify my
double getTotal()
function in linked list or what?
Topic archived. No new replies allowed.