2017-05-26 83 views
-1

我只是寫了一個數組類作爲Microsoft Visual Studio 2010中的練習,但我收到了一些惱人的錯誤。他們是:鏈接器錯誤。不知道這些錯誤意味着什麼

1>test.obj : error LNK2019: unresolved external symbol "public: __thiscall arrays<int>::~arrays<int>(void)" ([email protected]@@[email protected]) referenced in function _wmain 
1>test.obj : error LNK2019: unresolved external symbol "public: int & __thiscall arrays<int>::operator[](int)" ([email protected]@@[email protected]) referenced in function _wmain 
1>test.obj : error LNK2019: unresolved external symbol "public: bool __thiscall arrays<int>::operator==(class arrays<int> const &)const " ([email protected]@@[email protected]@Z) referenced in function _wmain 
1>test.obj : error LNK2019: unresolved external symbol "public: int const & __thiscall arrays<int>::operator=(class arrays<int> const &)" ([email protected]@@[email protected]@Z) referenced in function _wmain 
1>test.obj : error LNK2019: unresolved external symbol "public: __thiscall arrays<int>::arrays<int>(class arrays<int> const &)" ([email protected]@@[email protected]@@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 &)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected]@@@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 &)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected]@@@Z) referenced in function _wmain 
1>test.obj : error LNK2019: unresolved external symbol "public: int __thiscall arrays<int>::getsize(void)const " ([email protected][email protected]@@QBEHXZ) referenced in function _wmain 
1>test.obj : error LNK2019: unresolved external symbol "public: __thiscall arrays<int>::arrays<int>(int)" ([email protected]@@[email protected]@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; 
    } 

任何幫助,將不勝感激。

+0

如果你有拷貝粘貼了這段代碼,請在包含頭文件後從array.cpp中刪除';'cpp –

+0

['std :: vector <>'](http://en.cppreference.com/w/cpp/container/vector)已經爲你做了這項工作,使用它;有更好的方法來「實踐」。 –

回答

-1

模板類的實現代碼必須位於標題中,而不是位於.cpp文件中。這是必需的,以便使用您的模板類的其他代碼可以基於模板參數「實例化」它的代碼。 因此,只需將您的.cpp代碼全部移至您的.h文件中即可。

+1

這些問題有一個衆所周知的副本。請下次標記爲重複,而不是寫一個答案。 –

相關問題