链接器错误。不知道这些错误意味着什么

时间:2017-05-26 12:29:55

标签: c++

我只是在Microsoft Visual Studio 2010中编写一个数组类作为练习但是我遇到了一些烦人的错误。他们在这里:

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

我该如何修复它们? 这是我的代码:

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;
    }

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:-1)

模板类的实现代码必须位于标头中,而不是.cpp文件中。这是必需的,以便使用您的模板类的其他代码可以基于模板参数“实例化”它的代码。 因此,只需将您的.cpp中的所有代码移动到.h,就可以了。