添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams It might be because you are passing a copy of the variable Arr1 into Getdata rather than a reference to it. Andrew Truckle May 14, 2021 at 20:41

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);
                Why? To clear out the data left in the stream of course! Why else would you call a function named clear? To clear error flags?
– user4581301
                May 14, 2021 at 20:46
                @user4581301: Clearing error flags is what it does, if you want to flush the remaining data that would be ignore
– Ben Voigt
                May 14, 2021 at 20:59
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.