不能覺察Myclass.cpp:常量字符串值內容構造
const std::string FILE_NAME = "C:\\MyFileName.txt";
MyClass::MyClass()
{
ifstream fileToOpen;
fileToOpen.open(FILE_NAME); //FILE_NAME shows "" in debug
}
MyClass::OpenFile()
{
ifstream fileToOpen;
fileToOpen.open(FILE_NAME); //when called from another location, this resolves to proper file name.
}
我試圖打開在構造函數中的一個文件。當我在該文件名上調用open函數時,常量變量解析爲""
,但是如果函數在其他地方調用,則它工作得很好。我已經在.h文件中使用了相同的結果。誰能解釋爲什麼?
靜態初始化失敗? –
請**用[mcve]或[SSCCE(Short,Self Contained,Correct Example)](http://sscce.org)**你的問題 – NathanOliver
@Nathan - 我的例子有什麼問題。沒有使用大括號將它變成一個可編輯的課程,這基本上是我想要做的一切。 – Jason