operator<< richtig überladen?



  • 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
    
    #endif
    

    FileNotFoundException.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 😉


Anmelden zum Antworten