0

Im trying to run my Program but the compiler says it cannot open output file...

Here is My Code:

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);
    }

Im new to programming and using vscode so any help would be much appreciated.

I tried running it but it keeps telling me it cant compile and the following error:

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

I also moved it into a new folder and that didnt work neither. I also checked if main.exe was running but it was not. And I ran vscode via admin.

273K
  • 29,503
  • 10
  • 41
  • 64
Philipp
  • 11
  • 2
  • 6
    Maybe your program is still running from the last time you debugged or your antivirus is the problem. – drescherjm Apr 17 '23 at 17:26
  • ***And I ran vscode via admin*** That can cause permission problems. – drescherjm Apr 17 '23 at 17:27
  • @drescherjm I checked in the Task Manager and there was no main.exe running as I said above. I also tried to deactivate my antivirus so it shouldnt be a problem. – Philipp Apr 17 '23 at 17:28
  • 1
    Unrelated: `if(pos <= werte.size() && pos >=0) { werte[pos] = wert; }` That `<=` doesn't look right. if `pos` is `size()`, `werte[pos] = wert;` won't end well. – user4581301 Apr 17 '23 at 17:29
  • Open the output folder with your file explorer. See if the main.exe exists. See if you can delete it as the logged in user. – drescherjm Apr 17 '23 at 17:29
  • 2
    `C:\Users\phhep\OneDrive\Studium` OneDrive can be the cause of your problems. I have seen other questions in the past where OneDrive caused build problems like this. – drescherjm Apr 17 '23 at 17:30
  • Does this answer your question? [ld.exe: cannot open output file ... : Permission denied](https://stackoverflow.com/questions/7655471/ld-exe-cannot-open-output-file-permission-denied) – possum Apr 17 '23 at 17:43

2 Answers2

1

Actually just found out it was due to my antivirus (avira free) which sometimes decided to put my main.exe in quarantine. After that i couldnt run my program again. Fix for me was disabling/deinstalling my anti virus programm and moving the files to a new folder. (I guess you could just make an exception in your anti virus but I didnt really care to make that effort)

Philipp
  • 11
  • 2
0

If your process did not shutdown cleanly the last time you ran it, the .exe might still be locked by it even though the task doesn't show in Task Manager anymore. Try:

taskkill /f /im main.exe
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622