2012-11-19 183 views
1

我嘗試使用CString::GetAt()_tstoi()CString的最後一個字符轉換爲int,但編譯器返回我error C2065: '_tstoi' : undeclared identifier。我想我必須包括一個頭文件,我試過#include "tchar.h"沒有成功。錯誤嘗試使用_tstoi

CString str = "something2"; 
    int index = 0; 

    if (!str.IsEmpty()) 
    { 
     index = _tstoi((char*)str.GetAt(str.GetLength() - 1)); 
    } 

編輯:我用VC++ 6.0

+1

那麼,從'GetAt'函數開始返回一個_single'char'_而'_tstoi'需要一個C風格的字符串(即'char *')。 –

+0

要將單個字符'c'轉換爲整數,您可以簡單地使用'c - '0''。 –

+0

@JoachimPileborg做到了這一點,但編譯器從未到達那裏,因爲它無法識別'_tstoi'。我嘗試過使用'atoi',但是在運行時我想'atox.c'文件。 – MRM

回答

2

VC6下此功能是_ttoi而非_tstoi(仍包含在報頭 'TCHAR.H')

+0

使用'_ttoi'在運行時拋出一個'未處理的異常',要求我輸入'atox.c'的路徑 – MRM

+0

這是由於Joachim之前提到的,'GetAt'返回一個字符並將它轉換爲字符*。使用'char c = str.GetAt(str.GetLength() - 1);''index = _ttoi(&c);' – Liam

+0

現在沒問題,謝謝! – MRM

0

如果這是你應該糾正碼它像CString str =「something2」;

+0

對! – MRM