C++ 尝试将常量字符*转换为LPTSTR时链接器错误

C++ 尝试将常量字符*转换为LPTSTR时链接器错误,c++,visual-studio-2010,compiler-construction,compilation,linker,C++,Visual Studio 2010,Compiler Construction,Compilation,Linker,我有下面的代码片段,它将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()); } 链

我有下面的代码片段,它将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
错误19错误LNK1169:找到一个或多个多重定义符号STablUpd.exe 1 1 STablUpd
错误17错误LNK2005:“类std::basic_字符串*args”(?args@@3PAV?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@A)已在maindlg.obj tabledlg.obj STablUpd中定义
错误18错误LNK2005:“类std::basic_string*words”(?words@@3PAV?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@A)已在maindlg.obj tabledlg.obj STablUpd中定义
错误16错误LNK2005:“类std::basic_字符串cmdArgs”(?cmdArgs@@3V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@A)已在maindlg.obj tabledlg.obj STablUpd中定义
错误13错误LNK2005:“int u cdecl Main u OnInitDialog(struct HWND u*,struct HWND*,long)”(?Main _OnInitDialog@@YAHPAUHWND__@@0J@Z)已在maindlg.obj表dlg.obj STablUpd中定义
错误8错误LNK2005:“int\u stdcall Main\u DlgProc(struct HWND\uu*,unsigned int,unsigned int,long)”(?Main\u DlgProc@@YGHPAUHWND__@@IIJ@Z)已在maindlg.obj表dlg.obj STablUpd中定义
错误15错误LNK2005:“结构HINSTANCE\uuu*g\u hInstApp”(?g\u hInstApp@@3PAUHINSTANCE\uuuu@@A)已在maindlg.obj tabledlg.obj STablUpd中定义
错误9错误LNK2005:“已在maindlg.obj tabledlg.obj STablUpd中定义了void\uuu cdecl Main\u OnBrowse(struct HWND\uuu*)”(?Main\u OnBrowse@@YAXPAUHWND\uuuuz)
错误10错误LNK2005:“void\u cdecl Main\u OnClose(struct HWND\uuu*,int)”(?Main\u OnClose@@YAXPAUHWND__@@H@Z)已在maindlg.obj表dlg.obj STablUpd中定义
错误11错误LNK2005:“void u cdecl Main_OnCommand(struct HWND u*int,struct HWND*int,unsigned int)”(?Main_OnCommand@@YAXPAUHWND__@@H0I@Z)已在maindlg.obj表dlg.obj STablUpd中定义
错误12错误LNK2005:“void\u cdecl Main\u OnDisplay(struct HWND\uuu*)”(?Main\u OnDisplay@@YAXPAUHWND\uuuz)已在maindlg.obj tabledlg.obj STablUpd中定义
错误14错误LNK2005:_WinMain@16已在maindlg.obj表dlg.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;

错误消息清楚地说明了问题。您有多个(不是错误消息中显示的乘法)定义。您的错误与字符串转换没有直接关系。您正在错误地构建程序。从提供的信息来看,你是如何错误地构建它的是不可能的。试着描述一下你的程序中有哪些文件,你是如何编译它们的,哪些文件包括哪些其他文件等@JesseGood:消息正确地使用了乘法的副词形式。@AdrianMcCarthy:Dang,我不知道乘法可以用作副词。谢谢你纠正我。@john但是,当我不包括代码片段时,它编译得很好。