-1

i'm just writing an array class as practice in Microsoft Visual Studio 2010 but i'm getting some annoying errors. here they are:

1>test.obj : error LNK2019: unresolved external symbol "public: __thiscall arrays<int>::~arrays<int>(void)" (??1?$arrays@H@@QAE@XZ) referenced in function _wmain
1>test.obj : error LNK2019: unresolved external symbol "public: int & __thiscall arrays<int>::operator[](int)" (??A?$arrays@H@@QAEAAHH@Z) referenced in function _wmain
1>test.obj : error LNK2019: unresolved external symbol "public: bool __thiscall arrays<int>::operator==(class arrays<int> const &)const " (??8?$arrays@H@@QBE_NABV0@@Z) referenced in function _wmain
1>test.obj : error LNK2019: unresolved external symbol "public: int const & __thiscall arrays<int>::operator=(class arrays<int> const &)" (??4?$arrays@H@@QAEABHABV0@@Z) referenced in function _wmain
1>test.obj : error LNK2019: unresolved external symbol "public: __thiscall arrays<int>::arrays<int>(class arrays<int> const &)" (??0?$arrays@H@@QAE@ABV0@@Z) referenced in function _wmain
1>test.obj : error LNK2019: unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class arrays<int> const &)" (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$arrays@H@@@Z) referenced in function _wmain
1>test.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class arrays<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$arrays@H@@@Z) referenced in function _wmain
1>test.obj : error LNK2019: unresolved external symbol "public: int __thiscall arrays<int>::getsize(void)const " (?getsize@?$arrays@H@@QBEHXZ) referenced in function _wmain
1>test.obj : error LNK2019: unresolved external symbol "public: __thiscall arrays<int>::arrays<int>(int)" (??0?$arrays@H@@QAE@H@Z) referenced in function _wmain
1>c:\users\bm\documents\visual studio 2010\Projects\arrays\Debug\arrays.exe : fatal error LNK1120: 9 unresolved externals

how can I fix them? here is my code:

arrays.h

#ifndef ARRAYS_H
#define ARRAYS_H
#include <iostream>
using namespace std;
template <typename T>
class arrays{
    friend ostream &operator<<(ostream &output, const arrays &a);
    friend istream &operator>>(istream &input, const arrays &a);
    public:
        arrays(int = 10);
        arrays(const arrays &);
        ~arrays();
        int getsize() const;
        const T &operator=(const arrays &);
        bool operator==(const arrays &) const;
        bool operator!=(const arrays &right) const{
            return !((*this)==right);
        }
        T &operator[](int);
        T operator[](int) const;
    private:
        int size;
        T *ptr;
};
#endif

arrays.cpp

#include "stdafx.h"
#include <iostream>
#include "arrays.h"
#include <cstdlib>
using namespace std;
template <typename T>
arrays<T>::arrays(int mysize){
    size = mysize;
    ptr = new(int[size]);
    for(int i = 0; i< size; i++)
        ptr[i] = 0;
}
template <typename T>
arrays<T>::arrays(const arrays<T> &myarray){
    size = myarray.size;
    ptr = new(int[size]);
    for(int i = 0; i< size; i++){
        ptr[i] = myarray.ptr[i];
    }
}
template <typename T>
    arrays<T>::~arrays(){
        delete [] ptr;
    }
template <typename T>
    int arrays<T>::getsize() const {
        return size;
    }
    template <typename T>
    const T &arrays<T>::operator=(const arrays<T> &right){
        if ( &right != this){
            if(size != right.size){
                delete [] ptr;
                size= right.size;
                ptr = new(int[size]);
            }
            for(int i =0; i < size; i++)
                ptr[i] = right.ptr[i];
        }
        return *this;
    }
    template <typename T>
    bool arrays<T>::operator==(const arrays<T> &right) const{
        if(right.size != size)
            return false;
        for(int i = 0; i<size; i++)
            if(ptr[i] != right.ptr[i])
                return false;
        return true;
    }
    template <typename T>
    T &arrays<T>::operator[](int subscript) {
        if(subscript < 0 || subscript >= size){
            cout << "error: subscript out of range";

        }
        return ptr[subscript];
    }
    template <typename T>
    T arrays<T>::operator[](int subscript) const {
        if(subscript < 0 || subscript >= size){
            cout << "error: subscript out of range";
            exit(1);
        }
        return ptr[subscript];
    }
    template <typename T>
    istream &operator>>(istream &input, const arrays<T> &a){
        for(int i = 0; i< a.size; i++)
            input >> a.ptr[i];
        return input;
    }
    template <typename T>
    ostream &operator<<(ostream &output, arrays<T> &a){
        for(int i= 0; i<a.size;i++)
            output << a.ptr[i];
        return output;
    }

any help would be appreciated.

Milad
  • 67
  • 4

1 Answers1

-1

Implementation code for a template class must live in the header, not in the .cpp file. This is required so that other code that use your template class can "instanciate" it's code based on the template parameter. So just move all your code from your .cpp to your .h and you're good to go.

Eric Nicolas
  • 1,507
  • 3
  • 17
  • 24