I'm coding a constructor with a default argument as a enum for a test-example. The code compiles just fine using the Online GDB c++ compiler, but it's throwing a segmentation fault error when using the clang compiler we're supposed to use for the test, too.
In the below code snippet the error occurs when the first Wagon object is constructed, for some reason it doesn't occur with the latter one, which is why I've commented it out. I've also removed most of the other methods and references in the code, to narrow it down as far as possible.
I suppose the segmentation fault /invalid memory reference (SIGSEGV) occurs somehow due to how the runtime_error is called, but I'm not sure - can someone maybe give me a pointer?
Thanks in advance!
#include <iostream>
#include <stdexcept>
#include <vector>
#include <string>
using namespace std;
enum class Feature{Bar,Restaurant,Couchette,Standard,Toilet};
const std::vector<std::string> feature_names{"bar", "restaurant", "couchette", "standard","toilet"};
class Wagon {
int masse;
Feature einrichtung;
public:
Wagon(int m, enum Feature f = Feature::Standard){
if (m <5 || m > 100 ){throw runtime_error("mass to big or too small");}
this->masse = m;
this->einrichtung = f;
}
friend ostream &operator << (ostream &out, const Wagon &w){
out << "output text, call a few methods, etc";
}
};
int main(){
cout << "Basisfunktionalitaet\n\nWagon\n";
try {
cout << Wagon{5};
//cout << Wagon{4};
cout << '\n';
}
catch (runtime_error& e) {
cout << " Error1\n";
}
return 0;
}