权限被拒绝错误||无法打开输出文件 main.exe

问题描述 投票:0回答:1

我试图运行我的程序,但编译器说它无法打开输出文件...

这是我的代码:

main.cpp

#include "CMyVektor.h"

double myfunk(CMyVektor x)
{
    return sin(x[0]*x[1])+sin(x[0])+cos(x[1]);
}
int main()
{
    CMyVektor a(2);
    a.setComp(0,0.2);
    a.setComp(1,-2.1);
    std::cout << a[0] << " " << a[1] << std::endl;
    std::cout << a.getLength() << std::endl;
    std::cout << a.getDimension();
}

CMyVektor.h

#pragma once

#include <vector>
#include <iostream>
#include <cmath>

class CMyVektor
{
    private:

    std::vector<double> werte;

    public:

    CMyVektor(int dim)
    {
        werte.resize(dim);
    }
    int getDimension() const;
    void setComp(int pos, int wert);
    double operator [] (int pos) const;
    double getLength() const;
};

CMyVektor.cpp

#include <cmath>

#include "CMyVektor.h"


int CMyVektor::getDimension() const
    {
        return werte.size();
    }

    void CMyVektor::setComp(int pos, int wert)
    {
        if(pos <= werte.size() && pos >=0)
        {
            werte[pos] = wert;
        }
        else
        {
            throw std::out_of_range("Index out of range");
        }
    }

    double CMyVektor::operator[](int pos) const
    {
        if(pos <= werte.size() && pos >=0)
        {
            return werte[pos];
        }
        else
        {
            throw std::out_of_range("Index out of range");
        }
    }

    double CMyVektor::getLength() const
    {
        double sum = 0.0;
        for (double d : werte)
        {
            sum += (d*d);
        }
        return sqrt(sum);
    }

    CMyVektor operator+(CMyVektor a, CMyVektor b)
    {
        if(a.getDimension() == b.getDimension())
        {
            CMyVektor neu(a.getDimension());
            for(int i=0; i<a.getDimension();i++)
            {
                neu.setComp(i,(a[i] + b[i]));
            }
            return neu;
        }
        else
        {
            throw std::out_of_range("Not matching Dimensions");
        }
    }
    CMyVektor operator*(double lambda, CMyVektor a)
    {
        CMyVektor neu(a.getDimension());
        for (int i = 0; i < a.getDimension(); i++)
        {
            neu.setComp(i,lambda*a[i]);
        }
        return neu;
    }

    CMyVektor gradient(CMyVektor x, double (*funktion)(CMyVektor x)) 
    {
        CMyVektor grad(x.getDimension());
        double const h = 0.00000001;
        for (int i = 0; i< x.getDimension(); i++)
        {
            double gradWert = (funktion(x[i]+h)-funktion(x[i]))/h;
            grad.setComp(i,gradWert);
        }
        return grad;
    }
    
    CMyVektor gradientenVerfahren(CMyVektor x, double (*funktion)(CMyVektor x),double lambda = 1.0)
    {
        int count = 0;
        while (count <= 20 && gradient(x,funktion).getLength() >= 0.00001)
        {
            if(funktion(x+lambda)<=funktion(x))
            {
                x.setComp(0,x[0]+lambda);
                x.setComp(1,funktion(x));
                lambda /= 2;
            }
            else if(funktion(x+(lambda*2))>funktion(x+lambda))
            {
                x.setComp(0,x[0]+lambda*2);
                x.setComp(1,funktion(x));
                lambda *=2;
            }
            count ++;
        }
        return gradient(x,funktion);
    }

我是编程和使用 vscode 的新手,所以非常感谢任何帮助。

我尝试运行它,但它一直告诉我它无法编译并出现以下错误:

Starting build...
C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g C:\Users\phhep\OneDrive\Studium\2.Semester\HoeMa2\Praktika\1\neu\main.cpp -o C:\Users\phhep\OneDrive\Studium\2.Semester\HoeMa2\Praktika\1\neu\main.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot open output file C:\Users\phhep\OneDrive\Studium\2.Semester\HoeMa2\Praktika\1\neu\main.exe: Permission denied
collect2.exe: error: ld returned 1 exit status

我也将它移到一个新文件夹中,但也没有用。我还检查了 main.exe 是否正在运行,但没有。我通过管理员运行了 vscode。

c++ permission-denied
1个回答
0
投票

如果您的进程在上次运行时没有完全关闭,即使任务不再显示在任务管理器中,.exe 可能仍被它锁定。尝试:

taskkill /f /im main.exe
© www.soinside.com 2019 - 2024. All rights reserved.