我有以下代码片段,可将 const char * 转换为 LPTSTR,但是在编译时出现链接器错误。
char * pCopy3 = NULL;
if (sizeof(TCHAR) == sizeof(char))
{
size_t size = strlen(words[2].c_str());
pCopy3 = new char[size + 1];
strcpy(pCopy3, words[2].c_str());
}
链接器错误如下
Error 19 error LNK1169: one or more multiply defined symbols found STablUpd.exe 1 1 STablUpd
Error 17 error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * args" (?args@@3PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in maindlg.obj tabledlg.obj STablUpd
Error 18 error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * words" (?words@@3PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in maindlg.obj tabledlg.obj STablUpd
Error 16 error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > cmdArgs" (?cmdArgs@@3V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in maindlg.obj tabledlg.obj STablUpd
Error 13 error LNK2005: "int __cdecl Main_OnInitDialog(struct HWND__ *,struct HWND__ *,long)" (?Main_OnInitDialog@@YAHPAUHWND__@@0J@Z) already defined in maindlg.obj tabledlg.obj STablUpd
Error 8 error LNK2005: "int __stdcall Main_DlgProc(struct HWND__ *,unsigned int,unsigned int,long)" (?Main_DlgProc@@YGHPAUHWND__@@IIJ@Z) already defined in maindlg.obj tabledlg.obj STablUpd
Error 15 error LNK2005: "struct HINSTANCE__ * g_hInstApp" (?g_hInstApp@@3PAUHINSTANCE__@@A) already defined in maindlg.obj tabledlg.obj STablUpd
Error 9 error LNK2005: "void __cdecl Main_OnBrowse(struct HWND__ *)" (?Main_OnBrowse@@YAXPAUHWND__@@@Z) already defined in maindlg.obj tabledlg.obj STablUpd
Error 10 error LNK2005: "void __cdecl Main_OnClose(struct HWND__ *,int)" (?Main_OnClose@@YAXPAUHWND__@@H@Z) already defined in maindlg.obj tabledlg.obj STablUpd
Error 11 error LNK2005: "void __cdecl Main_OnCommand(struct HWND__ *,int,struct HWND__ *,unsigned int)" (?Main_OnCommand@@YAXPAUHWND__@@H0I@Z) already defined in maindlg.obj tabledlg.obj STablUpd
Error 12 error LNK2005: "void __cdecl Main_OnDisplay(struct HWND__ *)" (?Main_OnDisplay@@YAXPAUHWND__@@@Z) already defined in maindlg.obj tabledlg.obj STablUpd
Error 14 error LNK2005: _WinMain@16 already defined in maindlg.obj tabledlg.obj STablUpd
我假设您显示的代码片段将 std::string 转换为 char*。在这种情况下,代码是正确的,编译该代码时不应该有错误(strcpy 警告除外)。
您最初要求将 char* 转换为 LPTSTR。你可以这样做(对于 UNICODE 字符集):
char *test;
wchar_t *bla=new wchar_t[size];
swprintf(bla, size, L"%hs", test);
LPTSTR bla2=bla;
或者,如果您使用多字节字符集:
char *test;
LPTSTR bla2=test;