List Template Class Overloading << operator

Hello,

I've made a template class A that works as a general list of items. I need it to be possible to fill it with objects of a regular class B and to iterate through the list, printing each item.

example:
A List contains:
B object1
B object2
B object3

I have overloaded the << operator as a friend function to class B and have tried outputting objects of class B with a regular cout statement in the main function. This works. However, when I go to use the print function in template Class A on a list of items of Class B, using cout <<, the template function does not recognize that << has been overloaded for Class B. Am I going about this the wrong way? Is there a way to overload the << operator in the template class in such a general way? If anyone has an answer or a website to point me too I'd appreciate it.

The error I'm receiving (on Visual C++ 2008) is similar to this:
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const B' (or there is no acceptable conversion)
Last edited on
might be a const correctness problem. Hard to say without seeing any code.

Can you post the function containing the erroring line of code, as well as your << operator?
Sure thing; I think I can pinpoint the main problem areas.

Here is the header file for class B:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;

#ifndef B_H
#define B_H


class B
{
	public:
		B();
		string getName();
		void readName();
		friend ostream& operator<<(ostream& ost, B& emp);
	private:
		string name;
};

#endif 


Here is the function that overloads the << operator for class B:

1
2
3
4
5
ostream& operator<<(ostream& ost, B& emp)
{
	ost << emp.getName();
	return ost;
}


Here is the header file for template A:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <iostream>
using namespace std;

#ifndef A_H
#define A_H

template <class T, int maxSize>
class A
{
	public:
		A();
		void display() const;
		bool append(const T&);

	private:
		T elements[maxSize];
		int size;
};
#endif 


Here is the function that prints an instance of A:
1
2
3
4
5
6
7
8
9
10
11
template <class T, int maxSize>
void A<T, maxSize>::display() const
{
               // Display each list element.
	for (int i = 0; i < size; i++)
	{
		cout << elements[i];  //I assumed this would be overloaded
                                                                  //for objects of class B
		cout << endl;
	}
}


Here is the driver file where this is all used:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "A.h"
#include "B.h"
#include "A.cpp"
using namespace std;

int main()
{
	A<B, 15> myList;
	B objB;
	int numEmp;
                
               //some input to fill list with items temporarily stored in objB

	myList.display();


	return 0;
}
I was right... this is a const correctness problem.

See that A::display is const. Therefore anything that function does on its members (specifically, 'elements') must also be a const function.

Now note that B::operator << is non const.

The solution here is to make B const correct:

1
2
3
4
5
6
7
8
9
10
class B
{
	public:
		B();
		string getName() const;   //  <-- const
		void readName();  // I assume this sets 'name' and therefore isn't const
		friend ostream& operator<<(ostream& ost, const B& emp);  // <--- const B&
	private:
		string name;
};
Thanks a ton for the help; it's compiling and running properly now.
Topic archived. No new replies allowed.