so habe ich ein Problem mit diesem:Ausnahmen im Konstruktor C++
class Exception{
private : string message;
public :
Exception();
Exception(string str);
string what();
};
class Something{
public : Something(int m) throw(Exception);
....}
und in der CPP-Datei:
Exception::Exception()
{
message="cant divide by 0";
}
Exception::Exception(string str)
{
message = str;
}
Exception:string what()
{
return message;
}
und dann habe ich versucht, es in einigen Konstruktor zu verwenden
Something::Something(int m) throw(Exception)
{
if (m==0)
{
throw Exception();
}
};
und in der Hauptsache
...
catch(Exception obj){
cout<<obj.what();
und es zeigt "Exception" nennt keinen Typ und 'obj' wurde nicht erklärt und ich frage mich warum.
Ihre 'Exception' von' std :: exception' erben müssen in try/catch verwendbar zu sein ... – Charles
@ C650 Nein, Ausnahme ist völlig in Ordnung, so wie es ist . – 1201ProgramAlarm
Fügen Sie die Ausnahme-Header-Datei in Ihre Haupt-cpp-Datei ein? – 1201ProgramAlarm