operator<< richtig überladen?
-
Mein Compiler gibt mir bei folgenden Code:
ostream& operator<<(ostream &out, const FileNotFoundException &src) { out<<src.m_Filename; return out; };diese Warnung aus:
Warning 1 warning C4717: 'Vertexwahn::Exception::operator<<' : recursive on all control paths, function will cause runtime stack overflow
m_Filename ist ein std::string... mmh... was mache ich da falsch?
ich verstehe die Warnung des Compilers nicht - warum rekursiv? ich rufe ja da nicht wieder die gleiche Methode auf, sonderen eine Methode die einen String frist...?
-
du hast FileNotFoundException von std::string abgelitten
-
wisser schrieb:
du hast FileNotFoundException von std::string abgelitten
class FileNotFoundException { public: FileNotFoundException(string Filename); FileNotFoundException(const FileNotFoundException &src); friend ostream& operator<<(ostream &out, const FileNotFoundException &src); private: FileNotFoundException& operator=(const FileNotFoundException &src); string m_Filename; // Dateiname der Datei, die nicht gefunden wurde };
-
Bitte gib uns ein vollständigen, aber minimalen Sourcecode der die Warnung erzeugt und sag welchen Compiler du verwendest.
-
FileNotFoundException.h:
#ifndef FileNotFoundException_h #define FileNotFoundException_h #include <iostream> using namespace std; // <- pfui! ;) namespace Vertexwahn { namespace Exception { /// Diese Exception wird geworfen, wenn eine Datei nicht gefunden werden konnte. class FileNotFoundException { public: FileNotFoundException(string Filename); FileNotFoundException(const FileNotFoundException &src); friend ostream& operator<<(ostream &out, const FileNotFoundException &src); private: FileNotFoundException& operator=(const FileNotFoundException &src); string m_Filename; // Dateiname der Datei, die nicht gefunden wurde }; ostream& operator<<(ostream &out, const FileNotFoundException &src); } // namespace Exception } // namespace Vertexwahn #endifFileNotFoundException.cpp:
#include "FileNotFoundException.h" namespace Vertexwahn { namespace Exception { FileNotFoundException::FileNotFoundException(string Filename) : m_Filename(Filename) { } FileNotFoundException::FileNotFoundException(const FileNotFoundException &src) { m_Filename = src.m_Filename; } FileNotFoundException& FileNotFoundException::operator=(const FileNotFoundException &src) { return *this; } ostream& operator<<(ostream &out, const FileNotFoundException &src) { out<<src.m_Filename; return out; }; } }ich verwende MS Visual Studio 2005
-
Bitte mit main Funktion und alles in einer Datei.
-
main.cpp
#include <iostream> using namespace std; namespace Vertexwahn { namespace Exception { /// Diese Exception wird geworfen, wenn eine Datei nicht gefunden werden konnte. class FileNotFoundException { public: FileNotFoundException(string Filename); FileNotFoundException(const FileNotFoundException &src); friend ostream& operator<<(ostream &out, const FileNotFoundException &src); private: FileNotFoundException& operator=(const FileNotFoundException &src); string m_Filename; // Dateiname der Datei, die nicht gefunden wurde }; ostream& operator<<(ostream &out, const FileNotFoundException &src); FileNotFoundException::FileNotFoundException(string Filename) : m_Filename(Filename) { } FileNotFoundException::FileNotFoundException(const FileNotFoundException &src) { m_Filename = src.m_Filename; } FileNotFoundException& FileNotFoundException::operator=(const FileNotFoundException &src) { return *this; } ostream& operator<<(ostream &out, const FileNotFoundException &src) { out<<src.m_Filename; return out; }; } // namespace Exception } // namespace Vertexwahn int main() { cout<<"Hallo Welt!"<<endl; }Fehlermeldung:
Warning 1 warning C4717: 'Vertexwahn::Exception::operator<<' : recursive on all control paths, function will cause runtime stack overflow main.cpp 43
-
#include <string>
-
#include <string>
-
cool jetzt ist die Warnung weg

aber warum? er hätte doch einen Fehler melden müssen, weil er string nicht kennt... oder?g++ übersetzt es ohne mekern
-
wenn man den konstruktor entfernt lässt es sich gar nicht kompilieren.
das heißt also er baut über den Konstruktor ein FileNotFoundException und somit ist es rekursiv. mach den konstruktor mal explicit.
-
oh - jetzt wird mir das problem erst klar... implizite Typumwandlung.... mmh... ja das ist böse
