Anfängerfrage: mehrere Mains selektiv aufrufen



  • Das ist die control.h

    #include <LEDA/graphics/window.h>
    
    static leda::window *ControlWindowP;
    static double ControlWindowSpeed;
    void create_control()
    {
    ControlWindowP = new leda::window(260,60,"Remote Control");
    //panel_item pi=ControlWindowP->int_item("Speed",ControlWindowSpeed,95,105,1,"Animation Speed");
    ControlWindowP->buttons_per_line(3);
    ControlWindowP->button("Stop",41);
    ControlWindowP->button("Step",42);
    ControlWindowP->button("Cont",43);
    ControlWindowP->button("Slow",44);
    ControlWindowP->button("Normal",45);
    ControlWindowP->button("Fast",46);
    ControlWindowSpeed = 1.0;
    ControlWindowP->display(0,0);
    }
    void destroy_control()
    {
    ControlWindowP->close();
    delete ControlWindowP;
    }
    void control_wait(double t)
    {
    int val; double x,y;
    //while (ControlWindowP->get_event(val,x,y)!=no_event) ;
    int i;
    for (i=0; i<20; i++)
    	{
    	val = ControlWindowP->get_mouse(x,y);
    	//if (val!=NO_BUTTON) cout<<"ControlWindowP: " <<val <<endl;
    	switch(val) {
    	  case 41: ControlWindowSpeed=0.0; break;
    	  case 42: ControlWindowSpeed=-1.0; break;
    	  case 43: ControlWindowSpeed=1.0; break;
    	  case 44: ControlWindowSpeed=2.0; break;
    	  case 45: ControlWindowSpeed=1.0; break;
    	  case 46: ControlWindowSpeed=0.3; break;
    	  }
    	}
    if (ControlWindowSpeed==0.0)
    	{
    	while (ControlWindowSpeed==0.0)
    		{
    		val = ControlWindowP->get_mouse(x,y);
    	//if (val!=NO_BUTTON) cout<<"ControlWindowP: " <<val <<endl;
    		switch(val) {
    		  case 41: ControlWindowSpeed=0.0; break;
    		  case 42: ControlWindowSpeed=-1.0; break;
    		  case 43: ControlWindowSpeed=1.0; break;
    		  case 44: ControlWindowSpeed=2.0; break;
    		  case 45: ControlWindowSpeed=1.0; break;
    		  case 46: ControlWindowSpeed=0.3; break;
    		  }
    		}
    	}
    
    if (ControlWindowSpeed==-1.0)
    	{
    	ControlWindowSpeed=0.0;
    	}
    else
    	{
    #if __LEDA__ < 360
    	wait(t*ControlWindowSpeed);
    #else
    	leda_wait(t*ControlWindowSpeed);
    #endif
    	}
    }
    #define leda_wait control_wait
    

    Diese inkludiere ich sowohl in test1.c als auch in test2.c Über eine Main.c inkl. main(...) will ich nun selektiv (durch Auskommentieren) eine Methode aus test1 oder test2 aufrufen. Sobald sowohl test1 als auch test2 im Projekt sind, kommen beim Linken solche Fehler:

    "test2.obj : error LNK2005: "void __cdecl create_control(void)" (?create_control@@YAXXZ) ist bereits in test1.obj definiert." "fatal error LNK1169: Mindestens ein mehrfach definiertes Symbol gefunden."

    Die Sache mit den Guards habe ich wie gesagt schon ausprobiert, bringt aber nichts. Wenn ich test1 oder test2 aus dem Projekt raus werfe, dann klappts. Was ja aber gerade nicht der Sinn war 🙂

    Vielen Dank schonmal



  • Kannst du mir sagen, wieso du meine Posts nicht liest? 🙄

    Ich habe bereits erklärt, woran das liegt, und auch einen entsprechenden Lösungsvorschlag gebracht.



  • In wie fern habe ich dagegen verstoßen? Die control.h ist fix, die habe ich so bekommen und muss/will sie auch so verwenden. In meiner mains.h stehen in der Tat nur Deklarationen. Also was tun?



  • Entweder Include Guards in die control.h oder, was häßlicher ist, um jedes der includes.



  • Habe jetzt um die gesamte control.h einen Include Guard gemacht, gleiches Problem.



  • Wie sehen deine Include Guards denn aus?



  • Include Guards bringen nichts,
    denn zur Compile time ist die control.h ja nur
    einmal prro file included.
    Allerdings steht da jeweil in jeder testX.obj die definition der Funktionen darin.
    Die lösung heist also: Aufsplitten in Header und Implementation

    //! control.h
    #include <LEDA/graphics/window.h>
    
    extern static leda::window *ControlWindowP;
    extern static double ControlWindowSpeed;
    void create_control();
    void destroy_control();
    ...
    
    //! control.cpp
    #include "control.h"
    
    void create_control()
    {
      ControlWindowP = new leda::window(260,60,"Remote Control");
      //panel_item pi=ControlWindowP->int_item("Speed",ControlWindowSpeed,95,105,1,"Animation Speed");
      ControlWindowP->buttons_per_line(3);
      ControlWindowP->button("Stop",41);
      ControlWindowP->button("Step",42);
      ControlWindowP->button("Cont",43);
      ControlWindowP->button("Slow",44);
      ControlWindowP->button("Normal",45);
      ControlWindowP->button("Fast",46);
      ControlWindowSpeed = 1.0;
      ControlWindowP->display(0,0);
    }
    void destroy_control()
    {
      ControlWindowP->close();
      delete ControlWindowP;
    }
    ...
    

    mfg branleb



  • Danke, habs jetzt mal so umgebaut. Bei Zeile 4 und 5 deiner control.h sagt er "error C2159: Mehr als eine Speicherklasse angegeben"



  • Extern sagt aus, dass das Element in einer anderen Übersetzungseinheit (Sourcefile) definiert wird. Static besagt, dass das Element nur in dieser einen ÜE gültig ist. Das beisst sich natürlich ein wenig.

    Also musst Du die beiden Variablen entweder nur static nur in control.cpp notieren, oder aber extern in der control.h und ohne Speicherklasse nochmal in control.cpp.



  • sorry, das hab ich übersehen ...

    Da das Globale Variablen sind, sehe ich für static auch wenig sinn.
    Daher würde ich dir extern empfehlen.

    mfg branleb



  • Besten Dank für die Hilfe, es funktioniert jetzt wie gewünscht 🙂


Anmelden zum Antworten