-1

I have a linker error that I believe is related to ostream but I'm not sure. The error occurs on the last line of main(). I've spent a lot of time trying to fix this, if anyone comes up with anything I'll be grateful.

//NumericArray.h
//create numeric array

#ifndef NUMERICARRAY_H
#define NUMERICARRAY_H
#include "array.h"

namespace Joe
{
    namespace Containers
    {
        template<typename T>
        class NumericArray: public Array<T>
        {
        public:
            NumericArray<T>();    //default constructor
            NumericArray<T>(int i);    //constructor with one argument
            ~NumericArray<T>();    //destructor
            NumericArray<T>(const NumericArray<T>& source);    //copy constructor
            NumericArray<T>& operator = (const NumericArray<T>& arr1);    //assignment operator
            NumericArray<T>& operator * (double factor) const;    //scale
            NumericArray<T>& operator + (const NumericArray<T>& arr2) const;    //add
            T dotProduct(const NumericArray<T>& na) const;    //dot product
            friend ostream& operator << (ostream& os, const NumericArray<T>& t);    //send to ostream (friend)
        };
    }
}
#ifndef NUMERICARRAY_CPP
#include "NumericArray.cpp"
#endif
#endif

//NumericArray.cpp
//create numeric array
//mod dates:
//2.14.15

#ifndef NUMERICARRAY_CPP
#define NUMERICARRAY_CPP
#include "array.h"
#include "OutOfBoundsException.h"
#include "NumericArray.h"

using namespace Joe::CAD;

namespace Joe
{
    namespace Containers
    {
        template<typename T>
        NumericArray<T>::NumericArray() : Array<T>::Array()    //default constructor
        {
        }

        template<typename T>
        NumericArray<T>::NumericArray(const int i) : Array<T>(i) {}    //constructor with one argument


        template<typename T>
        NumericArray<T>::~NumericArray()    //destructor
        {
        }

        template<typename T>
        NumericArray<T>::NumericArray(const NumericArray& source) : Array<T>(source) {}    //copy constructor


        template<typename T>
        NumericArray<T>& NumericArray<T>::operator = (const NumericArray<T> &source)    //assignment operator
        {
            if (this == &source)
                return *this;
            Array<T>::operator = (source);
            return *this;
        }

        template<typename T>
        NumericArray<T>& NumericArray<T>::operator * (double factor) const    //scale
        {
            NumericArray<T> scale(Array<T>::Size());
            for (int i = 0; i < Array<T>::Size(); i++)
                scale[i] = factor * ((*this).GetElement(i));
            return scale;
        }

        template<typename T>
        NumericArray<T>& NumericArray<T>::operator + (const NumericArray<T>& arr) const    //add
        {
            if (Array<T>::Size() != arr.Size()) throw OutOfBoundsException();
            NumericArray<T> temp(Array<T>::Size());
            for (int i = 0; i < Array<T>::Size(); i++) {
                temp[i] = arr + (*this)[i];
            }
            return temp;
        }

        template<typename T>
        T NumericArray<T>::dotProduct(const NumericArray<T>& na) const    //compute dot product
        {
            double result = 0;
            for (int i = 0; i < Array<T>::Size(); i++)
                result += NumericArray<T>::GetElement(i) * (na.GetElement(i));
            return result;
        }

        template<typename T>
        ostream& operator << (ostream& os, const NumericArray<T>& t)    //send to ostream
        {
            os << t.NumericArray() << endl;
            return os;
        }
    }
}
#endif

//test.cpp
//testing templates

#include "array.h"
#include "point.h"
#include <iostream>
#include "ArrayException.h"
#include "OutOfBoundsException.h"
#include "NumericArray.h"

using namespace std;
using namespace Joe::Containers;

int main()
{
    NumericArray<double> doubArray1;    //default constructor

    NumericArray<double> doubArray2(3);    //initialize array
    for (int i = 0; i < 3; i++) {
        doubArray2[i] = rand() % 100 + 1;
        cout << doubArray2[i] << endl;
    } cout << endl;

    NumericArray<double> doubArray3(3);    //initialize array
    for (int i = 0; i < 3; i++) {
        doubArray3[i] = rand() % 100 + 1;
        cout << doubArray3[i] << endl;
    } cout << endl;

    NumericArray<double> doubArray4(doubArray3);    //test copy constructor
    cout << doubArray4.GetElement(1) << endl << endl;

    doubArray1 = doubArray2;    //test assignment operator
    for (int i = 0; i < 3; i++)
        cout << doubArray1[i] << endl;

    doubArray4 = doubArray2 * 3;
    cout << doubArray4 << endl;

    return 0;
}
2
  • I'd recommend posting the exact output (error) from your compiler as part of your post; also, what compiler you are using.
    – Thanatos
    Feb 16, 2015 at 5:03
  • 4
    "I have an error but I'm not going to tell you exactly what it is, but here is 150+ lines of code for you to sift through"...
    – John3136
    Feb 16, 2015 at 5:04

1 Answer 1

2
friend ostream& operator << (ostream& os, const NumericArray<T>& t);   

This declares a friend non-template operator<< function.

template<typename T>
ostream& operator << (ostream& os, const NumericArray<T>& t)    //send to ostream
{
    os << t.NumericArray() << endl;
    return os;
}

This defines a operator<< function template. The two are different.

Overload resolution ends up directing your << to the friend non-template function, and then the linker complains that it can't find a definition for it - because you provided none.

3
  • How can I resolve it? I thought since template<typename T> was on top of the class that it covered every function.
    – kits
    Feb 16, 2015 at 5:31
  • @kits There are various solutions in the linked duplicate. Or even consider removing the friend declaration entirely if operator<< doesn't need to access non-public members.
    – T.C.
    Feb 16, 2015 at 5:32
  • Thanks, I tried those solutions, but they end up generating errors for me.
    – kits
    Feb 16, 2015 at 5:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.