Search code examples
c++listinputistream

How to call the function that accepts std::istream&


I am a beginner in C++. What is the correct way to call a function that expects std::istream&?

Tried it with read(std::cin);, but I get an error from the compiler.

typedef double Element;

template<typename T>
std::list<T> read(std::istream& i) {  
  Element input;
  std::list<Element> l;
  while(i>>input) {
   l.push_back(input);
  }
  return l;
}

Solution

  • This is not related to the std::istream& parameter.

    The issue is that the function is a function template that requires an explicit template argument determining the type that is supposed to be read from the stream, e.g.:

    read<int>(std::cin)
    

    The error message from the compiler should be telling you something like that as well.

    That aside, you are then not using T in the function. Probably you wanted to replace all uses of Element by T and remove the typedef.