尝试将const char *转换为LPTSTR时链接器错误

时间:2012-11-01 21:10:42

标签: 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());
    }

链接器错误如下

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

1 个答案:

答案 0 :(得分:0)

我假设你显示的代码片段将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;