Heya again, sorry for the bombardment of threads I'm creating.
Once again I'm in need of your assistment as I only get an address printed out, which wasn't the intentions.
A pointer is an object that holds a single value; an address. If you pass a pointer to cout, you get that single value; an address. If you want cout to print out something else, like a string, you must pass it that string.
That LinkedList::toString function looks like what you need to get a string of all the contents.
Yes but.. according to the last thread I made here about this problem, I create the custom cout operator<< to take a pointer and then dereference it as seen here:
That's what I had expected too when I first tried to implement it.
If I move it inside the class I get a compiler error, saying that the method only accepts 1 argument and not 2 like I have.
I've always found operator overloading a pain and I've not much experience of it; I know that some overloads can be part of your class definition and some can't. What happens if you move your definition to above when you actually use it?
Edit: Hang on... if you overload an operator with a class definition, one of the two operands is assumed to be the class itself, so you only need to specify one operand. Could that be the cause of the error message you got when you put it in the class definition?
Alternatively, as it is now, but in the header, outside the actual class definition.
People will (or at least should) expect an address to be printed when they output a pointer. If they want the data to be printed they will output an object:
1 2 3 4 5 6 7 8 9
LinkedList* p = whatever;
cout << p; // this should print an address
cout << *p; // this should print whatever that address points to (data)
LinkedList obj;
cout << obj; // this should print the data
cout << &obj; // this should print an address
This is typical. It's what any C++ programmer would expect.
You are changing this behavior and making a pointer output data instead of the address. This is very confusing.
In file included from LinkedList.cpp:12:
LinkedList.h:23: error: ISO C++ forbids declaration of ‘ostream’ with no type
LinkedList.h:23: error: ‘ostream’ declared as an ‘inline’ field
LinkedList.h:23: error: expected ‘;’ before ‘&’ token
LinkedList.h:25: error: expected `;' before ‘private’
Yeah sorry -- I'm not reading your code closely enough.
ostream needs <iostream> to work. So do it like this:
linkedlist.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream> // <- include iostream here
#include "Node.h"
class LinkedList {
// class here
};
// then the operator here
// but be sure to use std:: prefix for ostream:
inline std::ostream& operator<< (std::ostream& outs, const LinkedList& obj) { return outs << obj.toString(); }
#endif
That should finally work.
Sorry I was only giving you half instructions before.
Ah! Ofcourse, didn't think of that!
Thank you for the help, it works now the way it should!
I am encountering another error however, don't know if I should start a new thread for it. Think I'm just gonna use this one instead. Basically, it just adds some values to the list and some are not added.
However, if I change "Nope" to "Byee" the output becomes:
[Hi, Yo]
and discard the 3rd Node.. I assume this has something to do with the insert method inside the LinkedList.cpp. Would you mind taking a look at it? I'm trying to figure it out but can't really do it