2016-10-08 2 views
0

내가 두 형태를 보여 strcmp는 0을 반환한다는 사실에도 불구하고 문자열 연결이 이상한 행동을 몰래 봤는데은 이상한 C++ 문자열 연결 동작

(가)

struct options = { 
    std::string ctifile; 
    ... 
}; 

을 다음과 같이 파일 options.h이 포함 동일 주 파일은 두 가지 방법으로

방법 1

#include "options.h" 
#include <string> 
#include "cantera/IdealGasMix.h" 

options opt = { 
    "mech/tot.cti" 
}; 

IdealGasMix gas(opt.ctifile, ...); 
에 기록되는 두 번째 방법은

, 어떤 이유

#include "options.h" 
#include <string> 
#include "cantera/IdealGasMix.h" 

options opt = { 
    "tot.cti" 
}; 

IdealGasMix gas(std::string("mech/") + opt.ctifile, ...); 

만 방법이 지정한 파일을 찾을 수 없습니다. 어떤 포인터? (말장난 의도)

+1

[작동하지 않아야하는 이유는 없습니다.] (http://cpp.sh/3yln) – amanuel2

+0

뭔가 다른 것이 잘못되어 게시 한 내용이 명확하지 않습니다. 문제를 보여주는 [mcve]를 찾아야합니다. – MicroVirus

+0

나를 위해 일합니다. 나는 당신에게 가스 함수 인수 선언을 의심한다. – Naidu

답변

1

을 strcmp는

std::string x = "blah blah"; 
assert(strcmp(x.c_str(), x.c_str()) == 0); 

을 이동하여 얻을 수 cstrings을 기대하지만 어쨌든 이유를 단지 표준을 사용하지 : 문자열을 표준 : : 문자열을 사용하는 경우 :

std::string x = "blah blah"; 
assert(x.compare(x) == 0); 
비교

http://www.cplusplus.com/reference/string/string/compare/

주의해야 할 점은 : 당신은 유니 코드 문자열을 비교하는 것을 시도하는 경우 다음 간단한을 strcmp는 항상

를 작동하지 않을 수 있습니다 0