简体   繁体   中英

Error including a .h file to the Win32 api in c++

I'm starting to learn how to use the Win32 Api to build a program un c++ and I've done a form that have a CheckBox that when you press it, the program(should) execute another code written in other .h The problem is that this code written in another cpp have strings, floats, unsigned int and char and when I include them in the main cpp where the form is written and try to build the program, it show me more than 17 errors(one by variable) I have been looking for answers and I've seen in this forum a guy how told that adding this

#define WIN32_LEAN_AND_MEAN

should work... I've tried it but keep happening.

Here you have the code:

#include <windows.h>
//Here I add the include, #include "newcpp.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

static char *title = TEXT("Check Box");

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    MSG  msg;
    WNDCLASS wc = { 0 };
    wc.lpszClassName = TEXT("Check Box");
    wc.hInstance = hInstance;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpfnWndProc = WndProc;
    wc.hCursor = LoadCursor(0, IDC_ARROW);


    RegisterClass(&wc);
    CreateWindow(wc.lpszClassName, title,
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        150, 150, 230, 150, 0, 0, hInstance, 0);

    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

    switch (msg)
    {
    case WM_CREATE:
    {
        CreateWindow(TEXT("button"), TEXT("Show Title"),
            WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
            20, 20, 185, 35,
            hwnd, (HMENU)1, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
        CheckDlgButton(hwnd, 1, BST_CHECKED);
        break;
    }

    case WM_COMMAND:
    {
        BOOL checked = IsDlgButtonChecked(hwnd, 1);
        if (checked) {
            CheckDlgButton(hwnd, 1, BST_UNCHECKED);
            SetWindowText(hwnd, TEXT(""));
            //Code Should be here like action();
        }
        else {
            CheckDlgButton(hwnd, 1, BST_CHECKED);
            SetWindowText(hwnd, title);
            //and here like anotheraction();
        }
        break;
    }

    case WM_DESTROY:
    {
        PostQuitMessage(0);
        break;
    }
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

And here you have a photo of my errors:

(Sorry about the language, I've been trying to translate VS to english but I dont know how ._. But I can translate all the errors if needed.)

在此处输入图片说明

Most of the errors say "already defined...." except the first one, that says "found one or more symbols defined simultaneously"

__Edited

The .cpp mostly have things like:

float CharToInt(char* value) {
    stringstream str;
    str << value;
    float x;
    str >> x;
    return x;
}

Here I've a test of my code. I put my function inside the main cpp and still give me this error.

Code:

#include <windows.h>
#include <iostream>
#include <string>
#include <psapi.h> //yes, so much libs, but I'll need them
#include <sstream>
#pragma comment(lib, "psapi")
using namespace std;
float CharToInt(char* value) {
    stringstream str;
    str << value;
    float x;
    str >> x;
    return x;
}

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

static char *title = TEXT("Check Box");

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    MSG  msg;
    WNDCLASS wc = { 0 };
    wc.lpszClassName = TEXT("Check Box");
    wc.hInstance = hInstance;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpfnWndProc = WndProc;
    wc.hCursor = LoadCursor(0, IDC_ARROW);


    RegisterClass(&wc);
    CreateWindow(wc.lpszClassName, title,
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        150, 150, 230, 150, 0, 0, hInstance, 0);

    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

    switch (msg)
    {
    case WM_CREATE:
    {
        CreateWindow(TEXT("button"), TEXT("Show Title"),
            WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
            20, 20, 185, 35,
            hwnd, (HMENU)1, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
        CheckDlgButton(hwnd, 1, BST_CHECKED);
        break;
    }

    case WM_COMMAND:
    {
        BOOL checked = IsDlgButtonChecked(hwnd, 1);
        if (checked) {
            CheckDlgButton(hwnd, 1, BST_UNCHECKED);
            SetWindowText(hwnd, TEXT(""));
            //Code Should be here like action();
        }
        else {
            CheckDlgButton(hwnd, 1, BST_CHECKED);
            SetWindowText(hwnd, title);
            //and here like anotheraction();
        }
        break;
    }

    case WM_DESTROY:
    {
        PostQuitMessage(0);
        break;
    }
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

Error: IMG

1-"found one or more symbols defined simultaneously" 2-"already defined...."

The problem is that you're inadvertently defining the same thing in multiple places.

You should #include header files, not .cpp files.

You should break your interface (struct definitions, function prototypes, typedefs, etc.) out into your header, and only leave function definitions in .cpp.

Your headers should always have an include guard

#ifndef MYHEADER_H
#define MYHEADER_H

struct mystruct {
    int i;
};

#endif 
/* MYHEADER_H */

See also:

==================================================

ADDENDUM:

If you do this:

// main1.cpp
#include <windows.h>
#include <iostream>

float CharToInt(char* value) {
    stringstream str;
    str << value;
    float x;
    str >> x;
    return x;
}

And then to do this:

// main.cpp
#include <windows.h>
#include "main1.cpp"  // BAD PRACTICE!!!!

int main (int argc, char *argv[]) {
  ...

THEN YOU'RE ASKING FOR TROUBLE! Don't do it!!!

Instead, you should do this:

// myheader.h
#ifndef MYHEADER_H
#define MYHEADER_H

#include <windows.h>
#include <iostream>

float CharToInt(char* value);
... // other function prototypes, struct definitions, typedefs, externs as needed

#endif
// MYHEADER_H

Unfortunately, even the "bad" code I described above won't necessarily cause a duplicate symbol error ... UNLESS you happened implement CharToInt() in both main1.cpp and main.cpp.

But it's just bad practice.

And I think the actual problem is something very similar to the "bad code" above.

I honestly believe if you:

a) Factored all your function prototypes and struct definitions into headers

b) Used an include guard in your headers

c) Implemented your functions in exactly one .cpp - making sure the function signature in the .cpp matches the function prototype in the .h header

... then the problem would "disappear".

I sincerely hope that helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM