[英]Error including a .h file to the Win32 api in c++
我开始学习如何使用 Win32 Api 来构建一个 un c++ 程序,我已经完成了一个带有 CheckBox 的表单,当你按下它时,程序(应该)执行另一个用其他 .h 编写的代码 问题是在另一个 cpp 中编写的这段代码有字符串、浮点数、无符号整数和字符,当我将它们包含在编写表单的主 cpp 中并尝试构建程序时,它显示了超过 17 个错误(一个是变量)我一直在寻找答案,我在这个论坛上看到一个人是如何被告知添加这个的
#define WIN32_LEAN_AND_MEAN
应该工作......我已经尝试过,但一直在发生。
代码如下:
#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);
}
在这里你有我的错误照片:
(对不起,语言,我一直在尝试将 VS 翻译成英语,但我不知道如何._。但如果需要,我可以翻译所有错误。)
大多数错误都说“已经定义......”,除了第一个,说“发现一个或多个同时定义的符号”
__编辑
.cpp 主要有以下内容:
float CharToInt(char* value) {
stringstream str;
str << value;
float x;
str >> x;
return x;
}
在这里,我对我的代码进行了测试。 我把我的函数放在主 cpp 中,但仍然给我这个错误。
代码:
#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);
}
错误: IMG
1-“发现一个或多个同时定义的符号” 2-“已经定义....”
问题是您无意中在多个地方定义了相同的东西。
您应该 #include 头文件,而不是 .cpp 文件。
您应该将您的接口(结构定义、函数原型、typedef 等)分解到您的头文件中,并且只将函数定义保留在 .cpp 中。
你的标题应该总是有一个包含保护
#ifndef MYHEADER_H
#define MYHEADER_H
struct mystruct {
int i;
};
#endif
/* MYHEADER_H */
也可以看看:
==================================================
附录:
如果你这样做:
// 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
不幸的是,即使我上面描述的“坏”代码也不一定会导致重复符号错误......除非你在 main1.cpp 和 main.cpp 中实现了 CharToInt()。
但这只是不好的做法。
而且我认为实际问题与上面的“坏代码”非常相似。
老实说,我相信如果您:
a) 将所有函数原型和结构定义分解为头文件
b) 在你的头文件中使用了一个包含保护
c) 在一个.cpp 中实现您的函数 - 确保 .cpp 中的函数签名与 .h 标头中的函数原型匹配
......那么问题就会“消失”。
我真诚地希望能帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.