Click to See Complete Forum and Search --> : std::string error combining *RESOLVED*
The_Dragon
Feb 24th, 2003, 12:11 PM
Ok i've a problem when using this code
#include <iostream>
#include <string>
using std::cout;
using std::string;
int main()
{
string recbuffer;
string endmsg1 = (char)(10);
string endmsg2 = (char)(13);
recbuffer += "NICK The_Dragon" + endmsg1 + endmsg2;
cout << recbuffer;
}
it's supposed to display "NICK DBZ_Dragon ASCII(10) ASCII(13) but it ends up giving an erro at string endmsg1... and endmsg2... so what am i doing wrong... im still fairly new to c++
twanvl
Feb 24th, 2003, 12:52 PM
First, in your case what you want to do should probably be done like this:
#include <iostream>
int main()
{
std::cout << "blahblah" << std::endl;
return 0;
}
The problem with your code lies in this line:
"NICK The_Dragon" + endmsg1;
you are trying to use "operator + (const char*, std::string)" (since the first operand is a C style string (const char*) and the second is a std::string). Although this function exists in the std namespace (in the <string> header), it is posible that your compiler doesn't recognize it, because it doesn't implement Koening lookup (a relatively new C++ feature) properly. The easiest way to avoid that problem is:
#include <string>
std::string function(const std::string& some_string)
{
return std::string("blahblah") + some_string;
}
The_Dragon
Feb 24th, 2003, 01:10 PM
after fixing stuff a bit I got this
#include <iostream>
#include <string>
using namespace std;
int main()
{
string recbuffer;
char endmsg1 = (char)(10);
recbuffer += "NICK DBZ_Dragon" + endmsg1;
cout << recbuffer << endl;
return 0;
}
there are no compile errors... only the output is 'ragon' instead of 'NICK DBZ_Dragon (ASCII10)'. I think it has something to with that string and char thing again I use gcc3.2.1 and glibc3.2.1 BTW
The_Dragon
Feb 25th, 2003, 03:15 AM
ok this is without the use of <string>
#include <iostream>
using namespace std;
int main()
{
const char * recbuffer;
recbuffer = "NICK DBZ_Dragon" + (char)(10);
cout << recbuffer << endl;
return 0;
}
output is still 'ragon' though... i really cant see why if i change 10 in 65 theres no output at all
The_Dragon
Feb 25th, 2003, 05:40 AM
OK fixed heres the new code
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
char recbuffer[256];
strcpy (recbuffer,"NICK Dragon");
strcat (recbuffer,"\x0a");
cout << recbuffer << endl;
return 0;
}
it's good enough I think
CornedBee
Feb 25th, 2003, 11:33 AM
gcc 3.2 does Koening lookup AFAIK.
The reason why this failed:
#include <iostream>
using namespace std;
int main()
{
const char * recbuffer;
recbuffer = "NICK DBZ_Dragon" + (char)(10);
cout << recbuffer << endl;
return 0;
}
lies in the nature of C strings. recbuffer is an address, not a string: there are no native strings in C. Thus the + operator is
(char *) + (char). (char) is promoted to (int) and normal pointer arithmetic is done. Which results in the passing of "ragon" to cout.
And contrary to what twanvl said the problem of the first code piece is NOT the cout line, but, as the compiler said, the string declarations. You can't initialize strings with a single character this way. The correct way is
string endmsg1(1, '\n');
where \n is the escape character for a newline, ASCII code 10. \r is the carriage return.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.