Variable verliert übergebenen Wert?



  • Hallo,

    vielleicht mag es eine komische Frage sein, aber ich als Newbie
    in VC++ blicke trotzdem nicht ganz durch.

    Ich habe in einer Klasse mehrere int-Variablen angelegt,
    die durch Funktionen der Klasse Werte zugewiesen bekommen.

    Wenn ich allerdings diese Werte in anderen Klassen benutzen möchte, Instanz der Klasse ist angelegt, sind die zugewiesenen Werte nicht vorhanden.
    Nur die, die ich im Konstruktor mit Werten belegt hatte.

    //Header
    class Init_App
    {
    public:
    	Init_App();
    	void Init_Grab_Size(unsigned char taste);
    ......
    
    public:
    	int xsize;
    	int ysize;
    	int zoom;		//1 normal/2 2time/3 3time
    
    	char *vconf;
    	int thresh;		//default value
    	int count;
    	int fullWindow;	//Fullscreen for CaptureWindow 1-yes/0-no
    };
    
    //cpp
    #include "initapp.h"
    
    Init_App::Init_App()
    {
    	vconf = new char[34];
    
    	thresh = 100;	//default value
    	count = 0;
    	fullWindow = 1;	//Fullscreen for CaptureWindow 1-yes/0-no
    }
    
    void Init_App::Init_Grab_Size(unsigned char taste)
    {
    	switch (taste) {
    		case '1' :
    			xsize = 192;
    			ysize = 144;
    			zoom = 4;
    			break;
                            .....
    		default:
    			.....
        }
    
    	sprintf(vconf,"-width=%d -height=%d -channel=%d",xsize ,ysize,1); 
    }
    
    //----------------------------------------------------andere klasse
    //Header
    
    class Main_App
    {
    public:
    	Main_App();
    	void Init();
    
    private:
    	OpenGL_App*	m_opengl_app;
    	VRML_App*	m_vrml_app;
    	Dialog_App*	m_dialog_app;
    	Init_App*	m_init_app;
    };
    
    //cpp
    #include "mainapp.h"
    
    Main_App::Main_App(): m_init_app(new Init_App),m_opengl_app(new OpenGL_App),m_vrml_app(new VRML_App),m_dialog_app(new Dialog_App)
    {
    
    }
    
    void Main_App::Init()
    {
    
    //hier steht die selbe sinnlose Zahl oder String drin
    	printf("\nvconf %s", m_init_app->vconf);
    	printf("\nxsize %d", m_init_app->xsize);
    	printf("\nysize %d", m_init_app->ysize);
    	printf("\nzoom %d", m_init_app->zoom);
    //hier sind die werte von dem konstruktor vorhanden
    	printf("\ncount %d", m_init_app->count);
    	printf("\nfullWindow %d", m_init_app->fullWindow);
    	printf("\nthresh %d", m_init_app->thresh);
            ....
    }
    


  • Rufst du die Funktion irgendwo auf?



  • ja,

    //Header
    #include "initapp.h"
    
    class Dialog_App
    {
    public:
    	Dialog_App();
    	void capture_size();
    
    	unsigned char taste;
    ..........
    
    private:
    	Init_App* m_init_app;
    ..........
    };
    
    //cpp
    #include "dialogapp.h"
    
    Dialog_App::Dialog_App(): m_init_app(new Init_App)
    {
    
    }
    void Dialog_App::capture_size()
    {
    	printf("*** Size of capture video for DV/CAM ***\n");
    	printf("\nPress (1) PAL 1/4\tStandard\t192x144");
    	printf("\nPress (2) PAL 3/8\tStandard\t288x216");
    	printf("\nPress (3) PAL 1/2\tCCIR601\t\t360x288");
    	printf("\nPress (4) PAL 1/2\tStandard\t384x288");
    	printf("\nPress (5) PAL FULL\tCCIR601\t\t720x576");
    	printf("\nPress (6) PAL FULL\tStandard\t768x576");
    	printf("\n");
    	printf("\nPress (7) VGA 1/2\tStandard\t320x240");
    	printf("\nPress (8) VGA FULL\tStandard\t640x480");
    	printf("\n-> ");
    	scanf("%s", &taste);
    	printf("\n");
    
    	m_init_app->Init_Grab_Size(taste);
    }
    ........
    


  • Aloha,

    ich tippe, Du rufst von Deiner MainApp()::Init(), bevor Du

    //hier steht die selbe sinnlose Zahl oder String drin 
        printf("\nvconf %s", m_init_app->vconf); 
        printf("\nxsize %d", m_init_app->xsize); 
        printf("\nysize %d", m_init_app->ysize); 
        printf("\nzoom %d", m_init_app->zoom); 
    //hier sind die werte von dem konstruktor vorhanden 
        printf("\ncount %d", m_init_app->count); 
        printf("\nfullWindow %d", m_init_app->fullWindow); 
        printf("\nthresh %d", m_init_app->thresh); 
            ....
    

    alles ausgibst irgendwo

    DialogApp::capture_size()
    

    Vorher legst Du noch als Membervariable eine neue

    m_init_app;
    

    Alles schön, aber Deine m_init_app aus Dialog_App ist nicht dieselbe, wie die in der Main_App.

    Lege Dir in Dialog_app nicht die Membervariable m_init_App neu an, sondern übergib nur den Zeiger aus der Main an den Dialog.

    Mit der rufst Du dann Init_grab_size auf, und alles wird gut...

    Grüße

    BOA


Anmelden zum Antworten