私は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任意の助けをいただければ幸いです
#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;
}
#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: はここに私のコードです。
このコードを貼り付けてコピーした場合は、arrays.cppからヘッダの後に ';'を削除してください –
['std :: vector <>'](http://en.cppreference.com/w/cpp/container/vector)は既にあなたのためにこの仕事をしています。 "練習する"より良い方法があります。 –