私の現在のプログラミングコースでは、任意のサイズのランダムに塗りつぶされた配列を作成するプログラムを作成しています。配列を含むクラスは、配列がcharのint OR値の値でいっぱいになるように、テンプレート化されていなければなりません。友だちの機能に未定義の参照があるのはなぜですか?
この時点で、私がやろうとしているのは、SafeArrayオブジェクトを印刷して、コードが正しく動作していることを確認できることです。残念ながら、オーバーロードされたストリーム挿入演算子(< <)で未定義の参照エラーが発生しています。
ご協力いただければ幸いです。ここで
は私のクラスの宣言です:
#include <iostream>
#include <cassert>
#include <typeinfo>
#include <cstdlib>
#include <ctime>
using namespace std;
template <class T>
class SafeArray
{
private:
T* pArr;
int size;
void create(int);
public:
SafeArray();
SafeArray(int);
SafeArray(const SafeArray<T> &);
~SafeArray();
SafeArray& operator=(const SafeArray<T> &);
SafeArray operator+(SafeArray<T> &);
SafeArray operator-(SafeArray<T> &);
int& operator[](int);
SafeArray& operator++();
SafeArray operator++(int);
int getSize() {return size; }
template <class U>
friend ostream& operator<<(ostream&, SafeArray<U> &);
template <class V>
friend istream& operator>>(istream&, SafeArray<V> &);
};
そして、私のオーバーロードされたストリームの挿入定義:
template <class T>
ostream& operator<<(ostream& out, const SafeArray<T> & arr)
{
for (int i = 0; i < arr.size; i++)
{
out << arr[i] << " ";
}
return out;
}
そして、私の小さなメイン:friendnessの宣言間のconst性の
#include "SafeArray.h"
#include <iostream>
using namespace std;
int main()
{
SafeArray<int> Safe(8);
cout << Safe;
return 0;
}
演算子関数定義はどこに配置しましたか? – user0042
@ user0042 SafeArray.cppでは、メインになるはずですか?クラスヘッダーの中で定義する必要がありますか? –
[テンプレートをヘッダーファイルにのみ実装できるのはなぜですか?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – user0042