Headerdateien



  • HAllo,
    ich habe gerade ein etwas größeres Projekt und wollte das ganze ein bisschen übersichtlich halten und habe mich dazu entschieden, mehrere Headerdateien zu benutzen.

    main.cpp inkludiert head.h

    head.h inkludiert

    #include <windows.h>
    #include <stdlib.h>
    #include <string.h>
    #include <tchar.h>
    #include <iostream>
    #include <commctrl.h>
    #include <string>
    #include <fstream>
    #include <sstream>
    #include "WndProc.h"
    #include "ChangeProc.h"
    

    In head.h gibt es eine Funktion

    void fieldClicked(int Field)
    	{
    	SendMessage(hWnd, WM_USER+1, 1, 1);
    	}
    

    und wird in main.cpp ausgerufen. Aber wenn ich das jetzt kompilen will, bekomme ich die Meldung "fieldClicked ist nicht bekannt". Ich verstehe nich wieso, ist doch alles miteinander verbunden.

    Danke schonmal!



  • Du zeigst leider nicht genug Code, um den Fehler nachvollziehen zu können (Siehe auch Link in meiner Signatur). Bitte ein vollständiges, compilierbares, aber aufs Wesentliche reduziertes Beispiel liefern.

    Davon abgesehen vermischst du munter C++- und C-Header. Wozu gleichzeitig <stdlib.h> (C-Header für Verarbeitung von rohen Zeichenketten, in C++ veraltet) und <string> (C++-Header für C++-Klasse zur Verarbeitung von Zeichenketten) einbinden? <stdlib.h> ist genauso veraltet...



  • Ist WinApi, geht ja aber um die Headerdateien:

    main.cpp:

    #include "head.h"
    
    int WINAPI WinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine,
                       int nCmdShow)
    {
    	InitCommonControls();
    	iniRaBLaK();
    
        WNDCLASSEX wcex;
    
        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.style          = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc    = WndProc;
        wcex.cbClsExtra     = 0;
        wcex.cbWndExtra     = 0;
        wcex.hInstance      = hInstance;
        wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
        wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName   = NULL;
        wcex.lpszClassName  = szWindowClass;
        wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    
        if (!RegisterClassEx(&wcex))
        {
            MessageBox(NULL,
                _T("Call to RegisterClassEx failed!"),
                _T("USM-Rechner"),
                NULL);
    
            return 1;
        }
    
    	wcex.lpszClassName	= _T("Change");
    	wcex.lpfnWndProc	= ChangeProc;
    
        if (!RegisterClassEx(&wcex))
        {
            MessageBox(NULL,
                _T("Call to RegisterClassEx failed!"),
                _T("USM-Rechner"),
                NULL);
    
            return 1;
        }
    
        hInst = hInstance; 
    
    		hWnd = CreateWindow(
            szWindowClass,
            szTitle,
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT,
            1500, 900,
            NULL,
            NULL,
            hInstance,
            NULL
        );
    
        if (!hWnd)
        {
            MessageBox(NULL,
                _T("Call to CreateWindow failed!"),
                _T("Win32 Guided Tour"),
                NULL);
    
            return 1;
        }
    
    		hWndChange = CreateWindow(
            _T("Change"),
            _T("Feld bearbeiten"),
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT,
            500, 500,
            NULL,
            NULL,
            hInstance,
            NULL
        );
    
        if (!hWndChange)
        {
            MessageBox(NULL,
                _T("Call to CreateWindow failed!"),
                _T("Win32 Guided Tour"),
                NULL);
    
            return 1;
        }
    
        ShowWindow(hWnd,
            nCmdShow);
        UpdateWindow(hWnd);
    
        // Main message loop:
        MSG msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return (int) msg.wParam;
    }
    

    WndProc.h:

    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        PAINTSTRUCT					ps;
        static						HDC hdc;
    	int							x, y;
    	static int					Breite, Höhe, Vert, Hor, Tief, Kugeln, VertC, HorC, TiefC;
    	char						buffer[256];
    	static std::string			KugelS, VertS, HorS, TiefS;
    	static bool					ac = false;
    	std::ostringstream			s;
    	static HWND					hButton[10], hEdit[10];
    	static HPEN					hPen;
    
        switch (message)
        {
    	case WM_LBUTTONDOWN:
                x = LOWORD( lParam );
                y = HIWORD( lParam );
    
    			if(x>=10 && x <= 160 && y>=10 && y<=110)
    			{
    				fieldClicked(1);
    			}
    			else if(x>=10 && x <= 160 && y>=110 && y<=220)
    			{
    				fieldClicked(2 );
    			}
    			else if(x>=10 && x <= 160 && y>=220 && y<=330)
    			{
    				fieldClicked(3 );
    			}
    			else if(x>=10 && x <= 160 && y>=330 && y<=440)
    			{
    				fieldClicked(4 );
    			}
    			else if(x>=10 && x <= 160 && y>=440 && y<=550)
    			{
    				fieldClicked(5 );
    			}
    
    			else if(x>=160 && x <= 310 && y>=10 && y<=110)
    			{
    				fieldClicked(6 );
    			}
    			else if(x>=160 && x <= 310 && y>=110 && y<=220)
    			{
    				fieldClicked(7 );
    			}
    			else if(x>=160 && x <= 310 && y>=220 && y<=330)
    			{
    				fieldClicked(8 );
    			}
    			else if(x>=160 && x <= 310 && y>=330 && y<=440)
    			{
    				fieldClicked(9 );
    			}
    			else if(x>=160 && x <= 310 && y>=440 && y<=550)
    			{
    				fieldClicked(10 );
    			}
    
    			else if(x>=310 && x <= 460 && y>=10 && y<=110)
    			{
    				fieldClicked(11 );
    			}
    			else if(x>=310 && x <= 460 && y>=110 && y<=220)
    			{
    				fieldClicked(12 );
    			}
    			else if(x>=310 && x <= 460 && y>=220 && y<=330)
    			{
    				fieldClicked(13 );
    			}
    			else if(x>=310 && x <= 460 && y>=330 && y<=440)
    			{
    				fieldClicked(14 );
    			}
    			else if(x>=310 && x <= 460 && y>=440 && y<=550)
    			{
    				fieldClicked(15 );
    			}
    
    			else if(x>=460 && x <= 610 && y>=10 && y<=110)
    			{
    				fieldClicked(16 );
    			}
    			else if(x>=460 && x <= 610 && y>=110 && y<=220)
    			{
    				fieldClicked(17 );
    			}
    			else if(x>=460 && x <= 610 && y>=220 && y<=330)
    			{		
    				fieldClicked(18 );
    			}
    			else if(x>=460 && x <= 610 && y>=330 && y<=440)
    			{
    				fieldClicked(19 );
    			}
    			else if(x>=460 && x <= 610 && y>=440 && y<=550)
    			{
    				fieldClicked(20 );
    			}
    
    			else if(x>=610 && x <= 760 && y>=10 && y<=110)
    			{
    				fieldClicked(21 );
    			}
    			else if(x>=610 && x <= 760 && y>=110 && y<=220)
    			{
    				fieldClicked(22 );
    			}
    			else if(x>=610 && x <= 760 && y>=220 && y<=330)
    			{
    				fieldClicked(23);
    			}
    			else if(x>=610 && x <= 760 && y>=330 && y<=440)
    			{
    				fieldClicked(24);
    			}
    			else if(x>=610 && x <= 760 && y>=440 && y<=550)
    			{
    				fieldClicked(25);
    			}
    
    			break;
    
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
    
            EndPaint(hWnd, &ps);
    		break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
            break;
        }
    
        return 0;
    }
    

    head.h:

    #include <windows.h>
    #include <stdlib.h>
    #include <string.h>
    #include <tchar.h>
    #include <iostream>
    #include <commctrl.h>
    #include <string>
    #include <fstream>
    #include <sstream>
    #include "WndProc.h"
    #include "ChangeProc.h"
    
    #pragma comment(lib, "comctl32.lib")
    
    static TCHAR szWindowClass[] = _T("win32app");
    
    static TCHAR szTitle[] = _T("zduizr");
    
    HINSTANCE hInst;
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    LRESULT CALLBACK ChangeProc(HWND, UINT, WPARAM, LPARAM);
    
    static HWND hWnd, hWndChange;
    
    // ---
    
    struct Feld {
    	int Tief;
    	int Hor;
    	int Vert;
    	bool klappe;
    	bool ac;
    
    	int conKon[9], 
    		conHor[5], 
    		conVert[5], 
    		conTief[5];
    };
    
    Feld *Felder = new Feld[26];
    
    bool	Konnektor[313];
    bool	Kugeln[73];
    
    bool	HorR[61];
    int		HorL[61];
    
    bool	TiefR[37]; 
    int		TiefL[37];
    
    bool	VertR[61];
    int		VertL[61];
    
    void iniRaBLaK()
    {
    	for(int x = 1;x<=313;x++)
    	{
    		Konnektor[x] = false;
    	}
    
    	for(int x = 1; x<=73; x++)
    	{
    		Kugeln[73] = false;
    	}
    
    	for(int x = 1; x<=61;x++)
    	{
    		HorR[x] = false;
    		HorL[x] = 0;
    
    		VertR[x] = false;
    		VertL[x] = 0;
    	}
    
    	for(int x = 1;x<=37;x++)
    	{
    		TiefR[x] = false;
    		TiefL[x] = 0;
    	}
    }
    
    	void fieldClicked(int Field)
    	{
    	SendMessage(hWnd, WM_USER, 1, 1);
    	}
    
    // ---
    


  • *seufz*
    Dir werden sicher gleich ein paar Leute einige Dinge sagen, die du falsch machst, aber dein konkretes Problem:

    Auch beim inkludieren der header ist prinzipiell die Reihenfolge wichtig.
    Wenn du head.h inkludierst, wird (nach ein paar anderen dateien, ich mags jetzt mal nicht "header" nennen) "WndProc.h" inkludiert. Dort rufst du die Funktion fieldClicked auf. Zu diesem Zeitpunkt hat der compiler aber noch nie von dieser Funktion gehört, und gibt einen Fehler aus. DANACH erklärst du ihm (ihm code von head.h), dass es diese Funktion gibt, dann ist es aber schon zu spät.

    Um das zu lösen, informier dich besser wie man header verwendet. Dann kommt der Rest wohl von alleine.



  • Der übliche Fehler in dem Zusammenhang sind zirkuläre includes, also A inkludiert B inkludiert C inkludiert A, in Verbindung mit include guards...Aber auch davon abgesehen ist es keine gute Idee, einfach überall alles zu inkludieren...


Anmelden zum Antworten