-2

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.)

enter image description here

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...."

Onelio
  • 313
  • 1
  • 4
  • 16

1 Answers1

3

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...

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • But I've not defined a variable in one cpp and in another, I just have the ones defined in the cpp that I want to include and the code that I've showed here – Onelio Mar 30 '16 at 19:32
  • 1
    If you've defined "foo" in a ."xcpp" ... and if you've #include'ed x.cpp in multiple places ... then you've defined "foo" multiple times. Please tell me you're not #including cpp files! That would be Bad! – paulsm4 Mar 30 '16 at 19:35
  • I just noticed that I've the first cpp with a include to the second one, then as you said I should have defined all the elements of the second cpp to the first one(form) by just including it(even if I've not defined them in the first one) and that is why it happens, right? One thing more, all of my variables are inside functions.. I'm just saying – Onelio Mar 30 '16 at 19:41
  • I've added part of my code to the initial cpp and still happens – Onelio Mar 30 '16 at 21:12
  • 1
    Dude, the LNK2005 is because you're (somehow) declaring the same symbol multiple times. Based on what you've said, it sounds like this is because you're #including a .cpp. If that's the case: *DON'T DO THAT*! Put your definitions (including function prototypes) in a *.h header*, and only ever include .h files, never .cpp files. When you create your header, don't forget to add a header guard. If you're still having problems after that, please update you post. – paulsm4 Mar 30 '16 at 21:23
  • Updated and still happening – Onelio Mar 30 '16 at 21:25
  • 1
    Q: can you tell us more about main.cpp and main1.cpp (where the duplication is occurring)? Or better, can you reproduce the problem with a simple two .cpp example: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – paulsm4 Mar 31 '16 at 04:03
  • I just tried to build the code without the "include" and worked fine. Then, I added to the main cpp the code of the second cpp and still gave me error (I've edited my post) – Onelio Mar 31 '16 at 18:16
  • Fixed (I thnkk) I've readed your post and I noticed that I added the old main too to the other project. But I still like to put it in other file, is there any way to make it "work" in other file as .h or .cpp or anyone...? – Onelio Mar 31 '16 at 18:55
  • Sure. Just put the "interface" (function prototypes, struct definitions. etc) in a .h header, and *only* put "implementation code" (function bodies) in a .cpp. #include .h, compile/link .cpp ... and you should be good! – paulsm4 Mar 31 '16 at 20:41