如何在C++中调试"cannot open output file allocating memory.exe: Permission denied"?



我正在学习如何分配内存。该代码应提示用户输入一些值,然后将值打印出来,以及所有这些值的总和。但是代码只会打印出值。它不会打印出总和。我正在使用Eclipse运行此代码,并且每当我尝试运行它时,都会收到消息"此代码中存在错误。您想继续启动吗?"。我找不到错误。

这是我每次运行时收到的消息。PS文件名是分配内存。

15:12:29 **** Incremental Build of configuration Debug for project allocating memory ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\allocating memory.o" "..\src\allocating memory.cpp" 
g++ -o "allocating memory.exe" "src\allocating memory.o" 
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/6.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot open output file allocating memory.exe: Permission denied
collect2.exe: error: ld returned 1 exit status

这是代码。谢谢

 #include <iostream>
    using namespace std;
    int main() {
        int i, j, sum = 0 ;
        int *p;
        cout<<"how many numbers do you want to input"<<endl;
                cin>> i;
            p =new (nothrow)int [i];
            if (p== nullptr){
                cout<<"Sorry not enough memory to allocate for this program"<<endl;}
            else {
                for (j=0; j<i; j++){
                cout<<"enter the numbers then "<<endl;
                cin>> p[j];}
            // to print the numbers
                cout << "the numbers you entered are"<<endl;
                    for (j=0; j<i;j++){
                        cout << p[j]<< "," << endl;
                        sum +=p[j];
                        cout << "the sum of all the inputs are"<< sum ;
                    }
                    delete[] p;
            }
        return 0;
    }

*****更新***

这是一个更好的尝试来解决这个问题,并且有效。我正在尝试动态分配用户输入的内存,打印出这些输入并找到其总和。这是代码。

#include <iostream>
using namespace std;
int main() {
    int i, j, sum = 0 ;
    int *p;
    cout<<"how many numbers do you want to input"<<endl;
            cin>> i;
        p =new (nothrow)int [i];
        if (p== nullptr){
            cout<<"Sorry not enough memory to allocate for this program"<<endl;}
        else {
            for (j=0; j<i; j++){
            cout<<"enter the numbers then "<<endl;
            cin>> p[j];}
        // to print the numbers
            cout << "the numbers you entered are"<< endl;
                for (j=0; j<i;j++){
                    cout << p[j]<< ",";
                    sum = sum + p[j];
                }
                cout << endl;
            cout << "The sum of these numbers is " << sum<< endl;

                delete[] p;
        }
    return 0;
} 

您在 = operator周围有错字。

sum =+ p[j];

应该是:

sum += p[j];

相关内容

最新更新