2012-04-16 41 views
-2

我想解決this問題,我得到這樣一個錯誤[從'字符'無效轉換''常量字符'],但我無法弄清楚如何解決它。下面是線,其中所述問題是:C++無效轉換從

Declarations: 
string alp("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); 
string formatted; 
char partoftext[20]; 
size_t found; 

found = text.copy(partoftext,2,0); 
partoftext[found] = '\0'; 
a = atoi(partoftext); 
formatted.append(alp[a]); 

...  

,問題是在這行代碼:

formatted.append(alp[a]); 

感謝。

+2

你不給足夠的上下文。什麼是格式化的?什麼是錯誤?什麼是? – 2012-04-16 07:26:53

+0

如何聲明'formatted'? – dschulz 2012-04-16 07:27:19

+0

我編輯過。對不起大家。 – ddacot 2012-04-16 07:30:54

回答

4

來源:HTTP://www.cplusplus.com/reference/string/string/append/

formatted.append(1, alp[a]); 

/* 
string& append (const string& str); 
string& append (const string& str, size_t pos, size_t n); 
string& append (const char* s, size_t n); 
string& append (const char* s); 
string& append (size_t n, char c); 
*/ 
+0

謝謝,就是這樣!現在它工作。 – ddacot 2012-04-16 07:39:53

+0

還有一個問題,爲什麼它顯示零而不是字母? http://ideone.com/92d5F – ddacot 2012-04-16 07:49:21

+0

它不是0而是'O'。你也可以使用supstr而不是複製和stringstream而不是atoi->或者看一下boost :: lexical_casthttp://stackoverflow.com/questions/8065413/stdlexical-cast-is-there-such-a-thing – AlexTheo 2012-04-16 08:10:12

0

如果我的猜測是正確的(這是一個猜測,因爲你沒有指定什麼錯誤實際上是),嘗試:

formatted.append(alp, a, 1);