2012-04-16 40 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);