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)
#include <iostream>
usingnamespace 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;
}
}
#include <iostream>
#include "A.h"
#include "B.h"
#include "A.cpp"
usingnamespace 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;
}