error C2509: "OnInitialUpdate": 成員函數沒有在 "CCteFormView" 中聲明
在標頭檔 CteFormView.h 類別定義中加入宣告:
class CCteFormView : public CFormView {
// ...
protected:
virtual void OnInitialUpdate(); // 必須加上 virtual
// ...
};
並確認 IMPLEMENT_DYNCREATE / DECLARE_DYNCREATE 宏正確。
error C2664: 無法將參數 2 從 "const char [1]" 轉換為 "LPCTSTR"
error C2664: 無法將參數 1 從 "const char [3]" 轉換為 "const wchar_t *"
專案設定 **Character Set = Use Unicode Character Set** (VS 預設)。
LPCTSTR → const wchar_t*,CString 內部為 wchar_t。
普通字串字面值 "abc" 為 const char* (ANSI),型別不符。
// 錯誤
m_ctlList.InsertItem(0, "");
str.Format("%d", i);
// 正確
m_ctlList.InsertItem(0, _T(""));
str.Format(_T("%d"), i);
// 或
m_ctlList.InsertItem(0, L"");
str.Format(L"%d", i);
CString 格式化字串同樣需 _T()。void CCteFormView::OnInitialUpdate() {
CFormView::OnInitialUpdate(); // 呼叫基底
GetParentFrame()->RecalcLayout();
ResizeParentToFit(); // 視需求
// 初始化 ListCtrl 欄位等
m_ctlList.InsertColumn(0, _T("項目"), LVCFMT_LEFT, 100);
}
補上虛函式宣告、統一使用 _T() 宏,即可解決 VS2012 MFC Unicode 專案常見編譯錯誤。