0
#include<iostream>
using namespace std;
int main()
{
 istream A;
 int a;
 A>>a;
}

here i am making object of istream class, to take input. but the compiler shows error which I can't understand. Please help me with it...

#include<iostream>
using namespace std;
class Car
{
 private:
    string name;
    string model;
    int engine;
    public:
    friend istream& operator>>(istream&, Car&);     
    friend ostream& operator<<(ostream&, Car);      
};istream& operator>>(istream &d, Car &e) {
    d>>e.name>>e.model>>e.engine;              
    return d;
}ostream& operator<<(ostream &d, Car e)      
{
    d<<e.name<<" "<<e.model<<" "<<e.engine;
    return d;
}
int main()
{
    Car a;
    Car b;
    cout<<"enter car credentials";
    cin>>a>>b;                        
    cout<<a<<b;
}

i want to overload the extraction and insertion operators >> << by myself, in my class. so here I have to make references of istream and ostream. so I want to know why I can't make objects of them. I'll add the complete code.. –

  • 1
    Take input from *what*? File? Console? String? – Yksisarvinen Mar 16 '21 at 09:08
  • what i understand is this that istream is a class. and cin is a predefined object of this class. in istream class there is a function defined as operator>>. now if I make an object of istream class(here i made object A of istream class) then that object will access its member function operator>>. so I should be able to take input using this object too. IF I HAVE MISTAKENLY UNDERSTOOD ANYTHING THEN PLEASE CLARIFY ME – Anshul Pareek Mar 16 '21 at 09:12
  • taking input from keyboard – Anshul Pareek Mar 16 '21 at 09:13
  • 1
    You misunderstood. `std::istream` can take input from various places, like files (`ifstream`), console (`cin`), or any generic memory. Your `istream` object takes input from nowhere – IWonderWhatThisAPIDoes Mar 16 '21 at 09:34
  • related: [Why is the construction of istreams is forbidden?](https://stackoverflow.com/questions/59118913/why-is-the-construction-of-istreams-is-forbidden) / [Why the constructor of std::ostream is protected?](https://stackoverflow.com/questions/18031357/why-the-constructor-of-stdostream-is-protected) – underscore_d Mar 16 '21 at 09:34
  • "_ want to overload the extraction and insertion operators >> << by myself, in my class. so here I have to make references of istream and ostream_" = correct. "_so I want to know why I can't make objects of them_" = does not compute. You have to justify _why_ you want to make objects of them, when the stdlib provides ready-made ones for all common purposes – underscore_d Mar 16 '21 at 11:03

2 Answers2

3

std::istream is a bit of an abstraction (although it's not an abstract class). It wraps some underlying memory buffer and exposes convenient methods to extract data from that buffer.

What is important is that it needs a buffer from which it can extract data. You must provide such buffer to create std::istream object, and creating such buffer manually is rather difficult.
This is why no one uses std::istream directly, but rather we use std::cin for reading input from console, std::ifstream for reading from files and std::istringstream for reading from strings.

You could extract underlying buffer from std::cin to initialize another std::istream (see it online)

int main()
{
    std::istream is {std::cin.rdbuf()};
    int a;
    is >> a;
    std::cout << a;
}

It is probably possible to avoid using std::cin and initialize it directly from stdin (the object that represents resource provided by the operating system), but I have no idea how do that.

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
  • your answer contains so many difficult words for me to understand. i have not yet studied abstract class, buffers... i don't understand these and also can you please explain what is the meaning of using namespace std; i read it at some places but i don't understand clear. – Anshul Pareek Mar 16 '21 at 09:42
  • 1
    @AnshulPareek The takeaway from my answer should be "don't go against the language, use `std::cin` or other ready classes". It is possible to create `std::istream` directly, but it doesn't make sense, unless you are working on some library to read from something completely new and previously unknown or you need to deal with a very strange operating system. – Yksisarvinen Mar 16 '21 at 10:01
  • @AnshulPareek Regarding `using namespace std;`, you'd first need to understand concept of namespaces in C++. In general, everything from the standard library lives in namespace `std`, so it will not mix with your own code. If you add `using namespace std;` to your code, all the names (and there are thousands of them) spill into your code, and compiler will get confused which name do you mean (do you want `sort()` from `std` or your own `sort`?). `using namespace std;` is generally considered [a bad practice](https://stackoverflow.com/q/1452721) because of those hard to detect and fix errors. – Yksisarvinen Mar 16 '21 at 10:06
  • i want to overload the extraction and insertion operators >> << by myself, in my class. so here I have to make references of istream and ostream. so I want to know why I can't make objects of them. I'll add the complete code.. – Anshul Pareek Mar 16 '21 at 10:23
0

Use the std::cin instance:

#include<iostream>

int main()
{
    int a;
    std::cin >> a;
}

Reference here: https://en.cppreference.com/w/cpp/io/cin

Johann Gerell
  • 24,991
  • 10
  • 72
  • 122