简体   繁体   中英

C++ Pointer to INT error?

Is there any method to use "cin" in c++ for a pointer to an INT? I mean

#include <iostream.h>
using namespace std;
int main()
{
int *n = new int;
cout << "Insert n: ";
cin >> n;
cout << n;
return 0;
}

The compiler says no match for the operator ">>"

Thank you. :)

Nope, you cannot read a pointer from user input using cin . As your compiler says, there is no overload for such operation.

However, if your intention is to read an int and assign the value of the memory pointed by n , you can instead pass a reference to the cin operator by dereferencing the pointer: cin >> *n .

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