Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将.h文件包含到c+;中的Win32 api时出错+; 我开始学习如何使用Win32 API来构建一个C++程序,我已经做了一个复选框,当你按下它时,程序应该执行另一个代码。 问题是,在另一个cpp中编写的代码包含字符串、浮点、无符号int和char,当我将它们包含在编写表单的主cpp中并尝试构建程序时,它向我显示了超过17个错误(一个由变量表示),我一直在寻找答案,我在这个论坛中看到一个家伙告诉我如何添加这些错误 #define WIN32_LEAN_AND_MEAN_C++_Api_Winapi_Compiler Errors - Fatal编程技术网

将.h文件包含到c+;中的Win32 api时出错+; 我开始学习如何使用Win32 API来构建一个C++程序,我已经做了一个复选框,当你按下它时,程序应该执行另一个代码。 问题是,在另一个cpp中编写的代码包含字符串、浮点、无符号int和char,当我将它们包含在编写表单的主cpp中并尝试构建程序时,它向我显示了超过17个错误(一个由变量表示),我一直在寻找答案,我在这个论坛中看到一个家伙告诉我如何添加这些错误 #define WIN32_LEAN_AND_MEAN

将.h文件包含到c+;中的Win32 api时出错+; 我开始学习如何使用Win32 API来构建一个C++程序,我已经做了一个复选框,当你按下它时,程序应该执行另一个代码。 问题是,在另一个cpp中编写的代码包含字符串、浮点、无符号int和char,当我将它们包含在编写表单的主cpp中并尝试构建程序时,它向我显示了超过17个错误(一个由变量表示),我一直在寻找答案,我在这个论坛中看到一个家伙告诉我如何添加这些错误 #define WIN32_LEAN_AND_MEAN,c++,api,winapi,compiler-errors,C++,Api,Winapi,Compiler Errors,应该有用。。。我试过了,但一直都是这样 这里有代码: #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 hPre

应该有用。。。我试过了,但一直都是这样

这里有代码:

#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);
}
#包括
//这里我添加了include,#include“newcpp.h”
LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);
静态字符*标题=文本(“复选框”);
int WINAPI WinMain(HINSTANCE HINSTANCE、HINSTANCE HPPreInstance、,
LPSTR lpCmdLine,int nCmdShow)
{
味精;
WNDCLASS wc={0};
wc.lpszClassName=文本(“复选框”);
wc.hInstance=hInstance;
wc.hbrBackground=GetSysColorBrush(颜色面);
wc.lpfnWndProc=WndProc;
wc.hCursor=加载光标(0,IDC_箭头);
注册类(&wc);
CreateWindow(wc.lpszClassName,title,
WS|U重叠窗口| WS|U可见,
150、150、230、150、0、0、0);
while(GetMessage(&msg,NULL,0,0)){
翻译信息(&msg);
发送消息(&msg);
}
返回(int)msg.wParam;
}
LRESULT回调WndProc(HWND HWND,UINT msg,WPARAM WPARAM,LPARAM LPARAM)
{
开关(msg)
{
案例WM_创建:
{
CreateWindow(文本(“按钮”)、文本(“显示标题”),
WS|U可见| WS|U儿童| BS|U复选框,
20, 20, 185, 35,
hwnd,(HMENU)1,((LPCREATESTRUCT)lParam)->hInstance,NULL);
检查按钮(hwnd,1,BST_已检查);
打破
}
case WM_命令:
{
BOOL checked=ISDLGBUTCHED(hwnd,1);
如果(选中){
选中DLG按钮(hwnd,1,BST_未选中);
SetWindowText(hwnd,文本(“”);
//代码应该像action()一样出现在这里;
}
否则{
检查按钮(hwnd,1,BST_已检查);
SetWindowText(hwnd,标题);
//这里就像另一个动作一样();
}
打破
}
案例WM_销毁:
{
PostQuitMessage(0);
打破
}
}
返回DefWindowProc(hwnd、msg、wParam、lParam);
}
这是我的错误的照片:

(对不起,我一直在尝试将VS翻译成英语,但我不知道如何翻译。但是如果需要,我可以翻译所有的错误。)

大多数错误都是“已定义…”,但第一个错误是“发现一个或多个同时定义的符号”

__编辑

.cpp主要包含以下内容:

float CharToInt(char* value) {
    stringstream str;
    str << value;
    float x;
    str >> x;
    return x;
}
float图表点(字符*值){
stringstream str;
str>x;
返回x;
}
这里我对我的代码进行了测试。我把我的函数放在主cpp中,但仍然给我这个错误

代码:

#包括
#包括
#包括
#包括//是的,有这么多的LIB,但我需要它们
#包括
#pragma注释(lib,“psapi”)
使用名称空间std;
浮点图表点(字符*值){
stringstream str;
str>x;
返回x;
}
LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);
静态字符*标题=文本(“复选框”);
int WINAPI WinMain(HINSTANCE HINSTANCE、HINSTANCE HPPreInstance、,
LPSTR lpCmdLine,int nCmdShow)
{
味精;
WNDCLASS wc={0};
wc.lpszClassName=文本(“复选框”);
wc.hInstance=hInstance;
wc.hbrBackground=GetSysColorBrush(颜色面);
wc.lpfnWndProc=WndProc;
wc.hCursor=加载光标(0,IDC_箭头);
注册类(&wc);
CreateWindow(wc.lpszClassName,title,
WS|U重叠窗口| WS|U可见,
150、150、230、150、0、0、0);
while(GetMessage(&msg,NULL,0,0)){
翻译信息(&msg);
发送消息(&msg);
}
返回(int)msg.wParam;
}
LRESULT回调WndProc(HWND HWND,UINT msg,WPARAM WPARAM,LPARAM LPARAM)
{
开关(msg)
{
案例WM_创建:
{
CreateWindow(文本(“按钮”)、文本(“显示标题”),
WS|U可见| WS|U儿童| BS|U复选框,
20, 20, 185, 35,
hwnd,(HMENU)1,((LPCREATESTRUCT)lParam)->hInstance,NULL);
检查按钮(hwnd,1,BST_已检查);
打破
}
case WM_命令:
{
BOOL checked=ISDLGBUTCHED(hwnd,1);
如果(选中){
选中DLG按钮(hwnd,1,BST_未选中);
SetWindowText(hwnd,文本(“”);
//代码应该像action()一样出现在这里;
}
否则{
检查按钮(hwnd,1,BST_已检查);
SetWindowText(hwnd,标题);
//这里就像另一个动作一样();
}
打破
}
案例WM_销毁:
{
PostQuitMessage(0);
打破
}
}
返回DefWindowProc(hwnd、msg、wParam、lParam);
}
错误:

1-“找到一个或多个同时定义的符号”
2-“已定义…”

问题是您无意中在多个位置定义了同一事物

您应该包括头文件,而不是.cpp文件

您应该将接口(结构定义、函数原型、typedef等)分解到标题中,并且只将函数定义保留在.cpp中

您的标题应始终具有

另见:

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

增编:

如果您这样做:

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

float CharToInt(char* value) {
    stringstream str;
    str << value;
    float x;
    str >> x;
    return x;
}
//main1.cpp
#包括
#包括
浮点图表点(字符*值){
stringstream str;
str>x;
返回x;
}
然后要做到这一点:

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

int main (int argc, char *argv[]) {
  ...
//main.cpp
#包括
#包括“main1.cpp”//坏习惯!!!!
int main(int argc,char*argv[]){
...
那你就是自找麻烦了!别这么做

相反,你
// main1.cpp
#include <windows.h>
#include <iostream>

float CharToInt(char* value) {
    stringstream str;
    str << value;
    float x;
    str >> x;
    return x;
}
// main.cpp
#include <windows.h>
#include "main1.cpp"  // BAD PRACTICE!!!!

int main (int argc, char *argv[]) {
  ...
// 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