Erorr: no operator “>>” matches these arguments

不羁的心 提交于 2019-12-14 03:26:50

问题


int main()
{ 
char* NamePointer = new char;


std::cout << "Enter the file you want to edit (MUST BE IN THE PROJECT'S DIRECTORY): ";
std::cin >> &NamePointer;

const char* FileName = NamePointer;
delete &NamePointer;


return 0;
}

This returns me the error in the title:

std::cin >> &NamePointer;

Any ideas? This is probably something simple I'm missing, but when I looked it up I didn't get my answer.

JUST FIXED IT, SORRY FOR BOTHERING! I took out the "&" in "&NamePointer".

Yeah I just found out another way of doing this using c_str(). Sorry!


回答1:


You should have:

std::cin >> NamePointer;

Also, you are just allocating an memory of one character, probably you meant:

char* NamePointer = new char[MAX_SIZE];

and dellocate it as:

delete []NamePointer;

Note that the, C++ way of doing this is to simply use std::string which takes care of the problem of buffer overflows you get with using char*:

std::string Name;
std::cin >> Name;



回答2:


There's no need for the address of operator here. Simply:

std::cin >> NamePointer;

Some other things I've noticed:

char* NamePointer = new char;

You probably want more space than just one character. A simple start may be a large static allocation:

char NamePointer[1024];

In which case, you omit the delete statement at the end (which also doesn't use an address of operator, by the way).



来源:https://stackoverflow.com/questions/10004499/erorr-no-operator-matches-these-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!