2017-03-21 1 views
0

Aus der Antwort here ich meine Klasse implementiert NotImplementedExceptionNotImplementedException C++

//exceptions.h 
namespace base 
{ 
    class NotImplementedException : public std::logic_error 
    { 
    public: 
     virtual char const* what() { return "Function not yet implemented."; } 
    }; 
} 

In einer anderen Klasse Ich mag würde die folgende Ausnahme (gleiche Namespace) werfen:

std::string to_string() override 
    { 
     throw NotImplementedException(); 
    } 

Die to_string Methode ist ein außer Kraft gesetzt Methode aus einer abstrakten Basisklasse.

namespace BSE { 
    class BaseObject 
    { 
     virtual std::string to_string() = 0; 
    }; 
} 

Leider ist die Zusammenstellung des vorherigen Code zeigt mir diesen Fehler:

error C2280: BSE::NotImplementedException::NotImplementedException(void)': attempting to reference a deleted function` 

Von here verstand ich, dass das Problem, es hat etwas mit Bewegung Konstruktor oder Zuweisung, die dies könnte zu cppreference.com - throw (1) nach zu tun der Fall sein:

First, copy-initializes the exception object from expression (this may call the move constructor for rvalue expression, and the copy/move may be subject to copy elision)

ich versuchte, indem

NotImplementedException(const NotImplementedException&) = default; 
    NotImplementedException& operator=(const NotImplementedException&) = default; 

meiner Klasse, aber das gibt mir

error C2512: 'BSE::NotImplementedException': no appropriate default constructor available 

und soweit ich weiß, std::logic_error hat keine Standard-Konstruktor definiert.

Q: Wie komme ich damit um?

+0

1. 'std :: logic_error' explizite Konstrukteuren hat. Du solltest einen von ihnen anrufen. –

+0

2. Lesen Sie dies: https://StackOverflow.com/Questions/10948316/Throw-New-Stdexception-VS-Throw-Stdexception –

+0

Ah, eigentlich die "neue" schlich da drin, weil ich herum spielte - das war nicht vorsätzlich. Und bedeutet das, dass die Implementierung, die ich gefunden habe, falsch ist? Oder sind mir verschiedene Umstände nicht bekannt? – Philipp

Antwort

3

sollte es sein, so etwas wie:

namespace base 
{ 
    class NotImplementedException : public std::logic_error 
    { 
    public: 
     NotImplementedException() : std::logic_error{"Function not yet implemented."} {} 
    }; 
} 

Und dann

std::string to_string() override 
{ 
    throw NotImplementedException(); // with new. 
} 
+0

Funktioniert jetzt, danke! – Philipp

Verwandte Themen