[英]How to use new & delete operators?
New & Delete Operators新建和删除运算符
On the Visual Studio Code editor, I tried to use the new and delete operators but the editor throws an error which I am unable to figure out.在 Visual Studio Code 编辑器上,我尝试使用 new 和 delete 运算符,但编辑器抛出了一个我无法弄清楚的错误。 I tried out everything to remove the error but all in vain!!!
我尝试了一切以消除错误,但都是徒劳的!!!
#include <iostream>
using namespace std;
int main()
{
int avg, *ptr1, *ptr2, *ptr3;
ptr1 = new int;
ptr2 = new int;
ptr3 = new int;
cout << "Enter the first number : ";
cin >> *ptr1;
cout << "Enter the second number : ";
cin >> *ptr2;
cout << "Enter the third number : ";
cin >> *ptr3;
avg = (*ptr1+*ptr2+*ptr3)/3;
cout << "Average is : " << avg << endl;
delete ptr1;
delete ptr2;
delete ptr3;
return 0;
}
The error message has been misinterpreted by the asker and has nothing to do the asker's use of with new
and delete
or any of the asker's C++ code (which could use some error checking on the IO but otherwise seems correct).该错误消息已被提问者误解,并且与提问者使用
new
和delete
或任何提问者的 C++ 代码无关(这可能会在 IO 上使用一些错误检查,但其他方面似乎是正确的)。 Visual Studio Code couldn't start compiling the code because the shell used to build the program could not consume the filename n&d.cpp because of the & in it. Visual Studio Code 无法开始编译代码,因为用于构建程序的 shell 无法使用文件名n&d.cpp ,因为其中包含& 。
Change the name to n_and_d.cpp or any other name that avoids using characters commonly used in programming as operators or delimiters.将名称更改为n_and_d.cpp或任何其他名称,以避免使用编程中常用的字符作为运算符或分隔符。 There will be similar problems in other build systems because of the spaces in the path leading to the source file.
由于指向源文件的路径中有空格,在其他构建系统中也会出现类似的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.