Re: [Dev-C++] RE : [c-prog] what's wrong with the code
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Daniel K. O. <dan...@ya...> - 2003-08-12 03:17:13
|
Dawood Vora wrote:
> int main()
> {
> char ch = 'z';
> string p = ch;
> /* for the above line the following is the error it gives me :
> conversion from `char' to non-scalar type
> `basic_string<char,string_char_traits<char>,__default_alloc_template<false,0>
> >' requested
> */
Long story short: a string can't be initialized from a 'char' like this.
From The Man himself (TC++PL 3rd, 20.3.4):
---
void f(char *p, vector<char>& v)
{
string s0; // ok: empty
string s00 = ""; // ok: empty too
string s1 = 'a'; // error
string s2 = 7; // error
string s3 (7); // error
srtring s4(7, 'a'); // OK: s4 = "aaaaaaa"
string s5 = "Frodo"; // OK: copy of "Frodo"
string s6 = s5; // OK: copy of s5
string s7(s5, 3, 2); // OK: s7 = s5[3] and s5[4] = "do"
string s8(p+7, 3); // OK: p[7], p[8] and p[9]
string s9(p, 7, 3); // OK: string(string(p), 7, 3)
string s10(v.begin(), v.end()); // OK: copy all chars from v
}
---
> p.push_back(ch);
> /* for the above line the following is the error it gives me :
> no matching function for call to
> `basic_string<char,string_char_traits<char>,__default_alloc_template<false,0>
> >::push_back (char &)'
> */
Try:
---
p+=ch;
---
Daniel K. O.
|