我开始学习如何使用Win32 Api构建一个程序un c ++并且我已经完成了一个具有CheckBox的表单,当你按下它时,程序(应该)执行另一个代码写在其他.h 问题是这个用另一个cpp编写的代码有字符串,浮点数,unsigned int和char,当我将它们包含在写入表单的主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翻译成英文,但我不知道如何._。但如果需要,我可以翻译所有错误。)
大多数错误都表示&#34;已定义....&#34;除了第一个,它说&#34;发现一个或多个符号同时定义&#34;
__被修改
.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 - &#34;发现一个或多个符号同时定义&#34; 2 - &#34;已定义....&#34;
答案 0 :(得分:2)
问题在于你无意中在多个地方定义了相同的东西。
你应该#include头文件,而不是.cpp文件。
你应该将你的接口(结构定义,函数原型,typedef等)分解到你的标题中,只留下.cpp中的函数定义。
您的标题应始终为include guard
#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
不幸的是,即便是&#34;坏&#34;我上面描述的代码不一定会导致重复的符号错误...除非你在main1.cpp和main.cpp中都实现了CharToInt()。
但这只是不好的做法。
我认为实际问题与“错误代码”非常相似。上方。
我真的相信如果你:
a)将所有函数原型和结构定义分解为标题
b)在标题中使用了包含保护
c)在一个 .cpp中实现你的函数 - 确保.cpp中的函数签名与.h头中的函数原型相匹配
......然后问题就会消失&#34;。
我衷心希望有所帮助...