简体   繁体   中英

std::string generates linker error — const char* does not. why?

just like the headline says.. i got this piece of code

std::string dir;
(ls == 1) ? dir = "Long" : dir = "Short"; 

which generates error i don´t understand

LNK2019: unresolved external symbol _CrtDbgReportW referenced in function 
"void * __cdecl std::_Allocate(unsigned __int64,unsigned __int64,bool)

when i switch to

const char* dir;
(ls == 1) ? dir = "Long" : dir = "Short"; 

all works pretty fine.

what is the deal there?

In

std::string dir;
(ls == 1) ? dir = "Long" : dir = "Short"; 

dir is a std::string , a fairly complex class that will be pulling in bits and pieces from all over the standard library, including memory allocation which appears to be calling a Windows debug helper function, _CrtDbgReportW , under some circumstances. For whatever reason, this debug helper function is not being linked.

But in

const char* dir;
(ls == 1) ? dir = "Long" : dir = "Short"; 

dir is just a simple pointer, an address. dir = "Long" simply points dir at the string literal "long" . This is just a simple assignment that requires no help from any libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM