I placed comments where I made changes. The main fix is to pass your object by reference, especially to Getdata()
. You passed a copy, put data into the copy, and when the function ended, the copy goes away and your original object was never touched.
#include <iostream>
using namespace std;
template <class T>
class Array {
public:
T U[10];
friend void DataOut(
const Array<string>&); // CHANGED: Need to take object by reference
friend void GetData(Array<string>&); // SAME
};
void DataOut(const Array<string>& Array1) { // SAME
cout << Array1.U[0];
}
void Getdata(Array<string>& Array1) { // SAME
cin >> Array1.U[0];
// cin.clear(); // CHANGED: Why?
}
int main() {
Array<string> Arr1;
Getdata(Arr1);
DataOut(Arr1);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…