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.