Home > front end >  How to call the function that accepts std::istream&
How to call the function that accepts std::istream&

Time:12-26

I am a beginner in c so thank you in advance for help. My question is: what is the correct way to call the function that expects std::istream&. Tried it with read(std::cin); , but I get error from 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;
}

CodePudding user response:

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.

CodePudding user response:

you have just a small syntax error:

try this code :

typedef double Element;
class test{

public:
auto read(std::istream& i){

Element input;
std::list<Element> l;
while(i>>input){
 l.push_back(input);
}
return l;
}


};


int main(){
test t;
t.read(std::cin);

return 0;
}
  • Related