-1
내 현재 프로그래밍 과정에서 임의의 크기로 무작위로 채워진 배열을 만드는 프로그램을 작성하고 있습니다. 배열을 포함하는 클래스는 템플릿의 int OR 값 (char 값)으로 채울 수 있도록 템플릿 화되어야합니다.친구 기능에 대한 정의되지 않은 참조가있는 이유는 무엇입니까?
이 시점에서, 내가하려는 것은 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