error LNK2001: 無法解析的外部符號 "class ATL::CStringT<wchar_t...> s"
專案設定為 **Unicode 字元集**,所有字串字面值必須加 L 前綴成為寬字串 wchar_t*。程式碼中 s = h + ":" + m + ":" + c; 的 ":" 為窄字串 char*,導致 CString 建構函式簽名不匹配,連結器找不到對應符號。
// 修正前
s = h + ":" + m + ":" + c;
// 修正後:每個字面值加 L
s = h + L":" + m + L":" + c;
// 或使用 _T 宏 (自動適應 Unicode/MBCS)
s = h + _T(":") + m + _T(":") + c;
void CNDlg::OnTimer(UINT_PTR nIDEvent) {
CTime t = CTime::GetCurrentTime();
CString str;
str.Format(L"%02d:%02d:%02d", t.GetHour(), t.GetMinute(), t.GetSecond()); // Format 也要 L
SetDlgItemText(IDC_curtime, str);
CDialogEx::OnTimer(nIDEvent);
if (alarmStr == str) { // alarmStr 為成員變數 CString
MessageBox(L"TIME'S UP!", L"鬧鐘", MB_OK | MB_ICONINFORMATION);
}
}
_T("text") 或 L"text"。CString 內部在 Unicode 專案為 CStringW,儲存 wchar_t。SetWindowTextW),MFC 宏自動映射。字串比較雖可行,但建議比較 CTime 或 int 時/分/秒,避免格式不一致問題:
if (t.GetHour() == alarmHour && t.GetMinute() == alarmMin && t.GetSecond() == alarmSec) { ... }