Geht ofstream als "private"



  • Hi,

    ich habe folgendes Problem:

    Ich bin dabei eine Klasse zu schreiben, die als Objekte Textdateien(VRML-Code) erstellt und diese fortführen kann.

    #include<fstream>
    #include<iostream>
    #include "Rotationparameter.h"
    #include "Cylinder.h"
    
    using namespace std;
    
    #ifndef _PAINTWRL_
    #define _PAINTWRL_
    class PaintWrl {
    private:
    	//kann man hier einen Stream als "private" deklarieren
            //z.B. so: static ofstream fileToWrl("MyFile.wrl");
    
    public:
    
    	/********** Defaultkonstruktor **********/
    	PaintWrl();
    
    	static void CylinderToWrl(Rotationparameter &MyRotation, Cylinder &MyCylinder);
    
    };
    #endif	// _PAINTWRL_
    

    Die dazugehörige CPP:

    //Datei mit dem Namen PaintWrl.cpp
    #include "PaintWrl.h"
    using namespace std;
    /********** Defaultkonstruktor **********/
    PaintWrl::PaintWrl()
    {
    	/********** Definition der VRML - Datei **********/
    	fileToWrl << "#VRML V2.0 utf8" << endl;
    	fileToWrl << "\nBackground" << endl;
    	fileToWrl << "\t{" << endl;
    	fileToWrl << "\tskyColor " << 1 << " " << 1 << " " << 1 << endl;
    	fileToWrl << "\t}" << endl;
    }
    
    static void CylinderToWrl(Rotationparameter &MyRotation, Cylinder &MyCylinder)
    {
        if(!fileToWrl.is_open())
           return;  
    
    	Point3d TransPoint;
    	TransPoint = MyCylinder.getStart();
    
            fileToWrl << "\nTransform" << endl;
    	fileToWrl << "\t{" << endl;
    	fileToWrl << "\t................................." << endl;
    }
    

    Dies wird dann so aufgerufen:

    //main.cpp
    ...
    PaintWrl Stephan;
    Stephan.CylinderToWrl(CylRotation, Testcylinder);
    

    Das funktioniert leider überhaupt nicht ...
    Bin für jedes praktische Beispiel Dankbar 😉



  • Stephan_lernt_C++ schrieb:

    class PaintWrl
    {
        //kann man hier einen Stream als "private" deklarieren
        //z.B. so: static ofstream fileToWrl("MyFile.wrl");
    };
    

    Ich hab's mal auf das wesentliche gekürzt 😛

    Das geht schon, aber nicht so. Zu ersteinmal solltest du dir bewusst machen, was static heißt. Bist du dir sicher, dass du das haben willst?

    Wenn ja, dann muss der Konstruktoraufruf (sprich die Definition) in einer(!) Implementierungsdatei stehen, in etwa so:

    #include "PaintWrl.hpp"
    // ...
    std::ofstream PaintWrl::fileToWrl ("MyFile.wrl");
    

    Ach ja, bitte NIEMALS "using namespace" im Header. Das macht das Klima kaputt.



  • Also ich meinte folgendes:

    //Test.h
    class Test{
    private:
    ofstream fileTest("MyFile.wrl");
    public:
    Test();
    void vielText(double a,double b);
    };
    
    //Test.cpp
    Test::Test()
    {
    fileTest << "Header..." << endl;
    }
    void vielText(double a,double b)
    {
    fileTest << "Werte für a " << a << endl;
    fileTest << "Werte für b " << b << endl;
    }
    
    //main.cpp
    Test Versuch_1;
    Versuch_1.vielText(c,d);
    

    Geht sowas? 😞



  • Nein. Der Konstruktoraufruf für fileTest /muss/ im Konstruktor von Test sein. Schließlich hat jedes Test-Objekt ein eigenes fileTest-Objekt, das initialisiert werden will.

    =>

    // Test.cpp
    Test () : fileTest ("MyFile.wrl")
    {
        // etc. pp.
    


  • Jetzt funktionierts 🙂


Anmelden zum Antworten