-1
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!

4

2 回答 2

3

你应该有:

std::cin >> NamePointer;

此外,您只是分配一个字符的内存,可能您的意思是:

char* NamePointer = new char[MAX_SIZE];

并将其分解为:

delete []NamePointer;

请注意,执行此操作的 C++ 方法是简单地使用std::stringwhich 来解决您使用时遇到的缓冲区溢出问题char*

std::string Name;
std::cin >> Name;
于 2012-04-04T03:25:24.503 回答
0

这里不需要运营商的地址。简单地:

std::cin >> NamePointer;

我注意到的其他一些事情:

char* NamePointer = new char;

您可能需要更多的空间而不仅仅是一个字符。一个简单的开始可能是一个大的静态分配:

char NamePointer[1024];

在这种情况下,您可以省略最后的delete语句(顺便说一下,它也不使用运算符的地址)。

于 2012-04-04T03:26:42.397 回答