I'm trying to create an template of an array class that encapsulates an array of a given type with several methods that are useful but nonstandard to regular c++ arrays. I have created a link struct to contain elements of the array and pointers to both the next and previous elements, and I am trying to overload the << operator to allow the value of a link to be printed easily simply by writing "cout << link_var" but I am having problems. Here is my code

#if !defined _LINK_
#define _LINK_

#if !defined _IOSTREAM_
#include _IOSTREAM_
#endif

typedef unsigned short int size_l;

template <typename T>
class list;

template <typename T>
class link
{
	typedef link<T>* link_p;

	T value;
	link_p next, previous;
public:
	link()
		: value(NULL), next(NULL), previous(NULL)
	{}

	link( T _value, link_p _next= NULL, link_p _previous= NULL )
		: value(_value), next(_next), previous(_previous)
	{}

	T rvalue() { return this->value; }

	link_p rnext() { return this->next; }

	link_p rprevious() { return this->previous; }

	const char* type() { return typeid(T).name(); };

	friend class list<T>;

	template <typename CharT>
    friend std::basic_ostream<CharT> &operator<< ( std::basic_ostream<CharT> &out, const link &_link )
	{
		out << _link.rvalue();

		return out;
	}
};

#endif /* _LINK_ */

Also, a little side not, I'm not sure if the type() method is the proper return type, code, etc. I would appreciate any help that can be offered, thank you.

Ok, I found a fix, I replaced the const link& with a regular link on line 40, but I don't understand why it can't be passed like that to avoid the recreation of the variable. If anyone could clear that up for me, I would appreciate it.

I'm sorry to keep throwing on questions, but I found one more question I have, is there a way to create a standard for operator function? For example, if I want something like this for operator declarations within a class to be able to work with all arithmetic operators (+, -, * and /)

T operator{operator type here}( T _value ) { return ( this->value {operator type here} _value ); }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.