Frage zu Klasse



  • Hallo ich habe mir dieses Programm geschrieben es funktioniert auch.

    Jedoch bekomme ich immer diese Linker Warnung mit ausgeben:

    [ILINK32 Warnung] Warning: Public Symbol '_pixel' ist in Modul C:\USERS\DR\DESKTOP\RUPT BOT\DEBUG\FILE1.OBJ und C:\USERS\us\DESKTOP\prog\DEBUG\PIXEL_COLOR.OBJ definiert

    Irgendwie kann ich mit der Warnung nichts anfangen ...

    Ok ich könnte jetzt sagen die Warnung is mir egal das Programm funktioniert ja. ^^

    Aber ich würde trotzdem gerne wissen was die bedeutet.

    main.cpp

    #include <windows.h>
    #include <iostream.h>
    #include <fstream.h>
    #include <conio.h>
    
    #include "pixel_color.h"
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    
    int ret=0;
    
    ret = pixel.get_pixel_color(20,20);
    
    cout<<ret<<endl;
    
    return 0;
    }
    

    pixel_color.cpp

    #include <iostream.h>
    #include <windows.h>
    #include "pixel_color.h"
    using namespace std;
    
    COLORREF pixel_class :: get_pixel_color ( int x , int y)
    {
    COLORREF farbe;
    HDC desktop;
    
    desktop = GetDC(0) ;
    
    farbe = GetPixel ( desktop ,  x , y );
    
    }
    
    return farbe;
    }
    

    pixel_color.h

    #ifndef PIXEL_CLASS_H
    #define PIXEL_CLASS_H
    
    class pixel_class
    {
    
    public:
    
    COLORREF get_pixel_color ( int x , int y );
    
    };
    
    pixel_class pixel;
    
    #endif
    


  • pixel_color.h:

    #ifndef PIXEL_CLASS_H
    #define PIXEL_CLASS_H
    class pixel_class {
    public:
      COLORREF get_pixel_color ( int x , int y );
    };
    
    // hier extern hinzufügen
    extern pixel_class pixel;
    
    #endif
    

    pixel_color.cpp:

    ...
    pixel_class pixel; // diese Zeile hinzufügen
    ...
    

    Das Problem ist, dass jedesmal, wenn der Header eingebunden wird, die Variable pixel erneut definiert wird und damit pro Datei auf eine andere Instanz zugegriffen wird. extern verhindert das und sagt bloss, dass so eine Instanz existiert und nicht neu erstellt werden muss.


Anmelden zum Antworten