Reputation: 21
In my current programming course we are writing a program to create a randomly filled array of any size. The class that contains the array must be templatized so that the array can be filled with values of int OR values of char.
At this point, all I'm trying to do is print out the SafeArray objects so I can make sure my code is working properly. Unfortunately I keep getting an undefined reference error on my overloaded stream insertion operator (<<).
Any help would be appreciated.
Here is my class declaration:
#include <iostream>
#include <cassert>
#include <typeinfo>
#include <cstdlib>
#include <ctime>
using namespace std;
template <class T>
class SafeArray
{
private:
T* pArr;
int size;
void create(int);
public:
SafeArray();
SafeArray(int);
SafeArray(const SafeArray<T> &);
~SafeArray();
SafeArray& operator=(const SafeArray<T> &);
SafeArray operator+(SafeArray<T> &);
SafeArray operator-(SafeArray<T> &);
int& operator[](int);
SafeArray& operator++();
SafeArray operator++(int);
int getSize() {return size; }
template <class U>
friend ostream& operator<<(ostream&, SafeArray<U> &);
template <class V>
friend istream& operator>>(istream&, SafeArray<V> &);
};
And my overloaded stream insertion definition:
template <class T>
ostream& operator<<(ostream& out, const SafeArray<T> & arr)
{
for (int i = 0; i < arr.size; i++)
{
out << arr[i] << " ";
}
return out;
}
And my small main:
#include "SafeArray.h"
#include <iostream>
using namespace std;
int main()
{
SafeArray<int> Safe(8);
cout << Safe;
return 0;
}
Upvotes: 0
Views: 1694
Reputation: 2167
discrepency of constness between declaration of friendness and your actual method signature ?
declaration of friendness :
template
friend ostream& operator<<(ostream&, SafeArray &);
implementation :
template ostream& operator<<(ostream& out, const SafeArray & arr);
Upvotes: 1