PDA

View Full Version : problem with C++ and strings..



Khao
November 7th, 2007, 08:35 PM
I can't figure out what's the problem with this : (don't mind all the server-related things, the only thing I have problem with is getting strings to work)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
//#include "include/Server.hpp"

#define PORT 1990 // port we're listening on

int main()
{
string MyString("hello");
//MyString= Encrypt(MyString);
printf("here's a string : %s\r", MyString);
return 0;
[... lots of code that works and is not using strings ... ]
}

the error I get when I try to compile is this :

Server.cpp: In function ‚int main()‚:
Server.cpp:15: error: 'string' was not declared in this scope
Server.cpp:15: error: expected `;' before 'MyString'
Server.cpp:16: error: 'MyString' was not declared in this scope

I'm a noob so please help me :(
Thank you!

aks44
November 7th, 2007, 08:44 PM
0. <string.h> is a non standard header. Use <string>

1. do you know what are namespaces? string belongs to the std namespace, ie. it must be written std::string

2. printf is not C++, it's C. Use std::cout :

#include <iostream> // also includes <string>
int main()
{
std::string str = "Hello world!";
std::cout << "I'm saying: " << str << std::endl;
return 0;
}

If you really MUST use printf (why would you?) you gotta remember that %s requires a (const?) char*, so you have to use str.c_str()

And BTW, line-feed is \n, carriage-return is \r, on *nix only \n is needed to get a new line. Unless you wanted to go back to first column without actually having a new line.

Khao
November 7th, 2007, 08:49 PM
Thank you very much :) As I said, I'm a noob, this messy thing came from trying to make it work using a lot of different source codes and things and it got all messy :P

aks44
November 7th, 2007, 08:52 PM
Maybe you may want to read this very short introduction into namespaces (http://www.winterdom.com/dev/cpp/nspaces.html)?

Khao
November 7th, 2007, 09:05 PM
that's pretty useful, but I'm having another little problem here : how do I use methods and functions with a string object? I tried MyString.lenght() as it showed in a tutorial and it returned this error :
error: 'truct std::string' has no member named 'lenght'

aks44
November 7th, 2007, 09:14 PM
I tried MyString.lenght() as it showed in a tutorial and it returned this error :
error: 'truct std::string' has no member named 'lenght'

Because it is named length() not lenght() ;)

Khao
November 7th, 2007, 09:15 PM
haha that's silly of me :P thank you very much!