简体   繁体   中英

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

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;
}

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, eg:

read<int>(std::cin)

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM