Thread: [Dev-C++] RE : [c-prog] what's wrong with the code
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Dawood V. <da...@ku...> - 2003-08-12 01:39:49
|
Subject: RE : [c-prog] what's wrong with the code
I tried "using namespace std;" it still gives me error.
I tried: string p(1,ch); But it does not serve the purpose of the
code, Basically, I want to enter one character at the back of the string
at one time.
Lastly, it simply does not compile: below is the same code, I produce
again, with the type of compile time error it gives. The details about the
errors are in the comments of the code just below the line which is
responsible for the error. I need two things. Explanation and alternative.
#include <iostream.h>
#include <stdlib.h>
#include <string>
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
*/
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 &)'
*/
system("PAUSE");
return 0;
}
|
|
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.
|
|
From: Ioannis V. <iv...@em...> - 2003-08-12 14:54:41
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Dawood Vora
> Sent: Tuesday, August 12, 2003 4:21 AM
> To: Dev C++
> Subject: [Dev-C++] RE : [c-prog] what's wrong with the code
>
>
> Subject: RE : [c-prog] what's wrong with the code
>
> I tried "using namespace std;" it still gives me error.
> #include <iostream.h>
#include <iostream>
But you do not use any facilities of <iostream> in your code.
> #include <stdlib.h>
Better: #include <cstdlib>
> #include <string>
>
>
> int main()
> {
using namespace std;
> 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_te
> mplate<false,0>
> >' requested
> */
The above line is wrong. There is no string constructor taking a char as an
argument.
So your code becomes:
#include <cstdlib>
#include <string>
int main()
{
using namespace std;
char ch = 'z';
string p;
p.push_back(ch);
system("PAUSE");
return 0;
}
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
|
|
From: <a_g...@ya...> - 2003-08-12 19:53:12
|
string p =ch; file://does not make much sense to me!!!
Ioannis Vranos <iv...@em...> wrote:> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Dawood Vora
> Sent: Tuesday, August 12, 2003 4:21 AM
> To: Dev C++
> Subject: [Dev-C++] RE : [c-prog] what's wrong with the code
>
>
> Subject: RE : [c-prog] what's wrong with the code
>
> I tried "using namespace std;" it still gives me error.
> #include
#include
But you do not use any facilities of in your code.
> #include
Better: #include
> #include
>
>
> int main()
> {
using namespace std;
> 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,__default_alloc_te
> mplate
> >' requested
> */
The above line is wrong. There is no string constructor taking a char as an
argument.
So your code becomes:
#include
#include
int main()
{
using namespace std;
char ch = 'z';
string p;
p.push_back(ch);
system("PAUSE");
return 0;
}
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
Thanx and Regards
Avik Ghose
Win TVs, Bikes, DVD players and more!Click onYahoo! India Promos |