Overload + operator to append node to linked list

I wrote the code to overload the + operator to append a book class node to a bookshelf class linked list, and the compiler says there aren't any errors with it. But when I try to add 2 book objects in the main program it says that there "no operator + that matches these operands" and "binary +: no operator found which takes a right-hand operand of type 'book'(or there is no acceptable conversion)". I'm thinking it has something to do with the function headers rather than the actual code, since it won't even compile.
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once
#ifndef _BOOKSHELF_H_INCLUDED_
#define _BOOKSHELF_H_INCLUDED_

class book;

#include <iostream>

class bookshelf
{
private:
	book* head;
	book* next;

public:
	bookshelf();
	void removeBook(book*);
	book* getNext();
	void setNext(book*);
	book* getBook();
	int getNumBooks();
	void operator+(book&);
};

#endif // !_BOOKSHELF_H_INCLUDED_


void bookshelf::operator+(book &obj)
{
	book* iter;
	if (head == nullptr)
	{
		head = &obj;
		head->bookshelf::setNext(nullptr);
	}
	else
	{
		iter = head;
		while (iter->bookshelf::getNext() != nullptr)
		{
			iter = iter->bookshelf::getNext();
		}	
		iter->bookshelf::setNext(&obj);
	}
}


#include <iostream>
#include "bookshelf.h"
#include "book.h"
#include "chapter.h"
#include "paragraph.h"

int main()
{
	bookshelf* shelf = new bookshelf();
	char t[] = "Harry Potter";
	char a[] = "J.K. Rowling";
	char* title = new char[100];
	char* author = new char[100];
	strcpy_s(title, 100, t);
	strcpy_s(author, 100, a);
	
	// Add book to shelf
	book* newBook = new book(title, author);
	book* head = nullptr;

	head = *head + *newBook;
}	
void bookshelf::operator+(book &obj)

This operator can be used to add a book to a bookshelf.

Let's see what you're trying to add:
*head + *newBook;

head is a pointer-to-book, so *head is a book.
newBook is a pointer-to-book, so *newBook is a book.


You wrote an operator that works on a bookshelf and a book.
You're trying to use an operator that works on a book and a book; no such operator exists, hence the compiler complaining.
1
2
book* head = nullptr;
head = *head 


This is disastrous, by the way. Dereferencing a null pointer is a very bad idea.
Oh I see what you mean! Thank you so much, I understand how the overloaded operator works now. And thank you for the tip!
Topic archived. No new replies allowed.