PDA

View Full Version : cant declare a string variable...


geekgod
11-08-2006, 10:16 AM
hello..
i've just recently begun withh c++ programming in linux, and i am using debian sarge(stable) and gcc 3.3.
my problem is that i am nor being able to use string variables, whenever i declare a string variable with
string *variablename*..
the compiler returns an error "variable string not declared".
i'd included the string.h header file..and i tried bot
#include <strings> and
#include <strings.h>

i'd searhed the g++ 3.3 include diretory and the header file is present there..but still i cant declare a string variable...

please help..

Sykora
11-08-2006, 10:27 AM
I think it is #include <string>, not <strings>

arunks
11-08-2006, 11:00 AM
yes it is string.h

Sykora
11-08-2006, 11:28 AM
If he's using g++, then saying #include <string.h> will chuck an error. Don't put the ".h", that's c-style. just say #include <string> and leave it at that.

Venom
11-08-2006, 11:36 AM
He's said he tried both...

Sykora
11-08-2006, 11:40 AM
He tried "strings", not "string"

geekgod
11-08-2006, 04:12 PM
well i dont exactly remember whether i used <string> or <strings>..but i had used the corect one, coz i used the name of the file that was present in the include directory of g++ 3.3.
and also, i remember this much that i had used both with .h and without .h. without .h it couldnt recognise even the cout and cin functions...:-(..which means it could recognise the declaration for iostream.h..

so..what do i do?

Sykora
11-08-2006, 04:25 PM
Have you included the namespace statement?

geekgod
11-08-2006, 11:15 PM
@Sykora:..u mean "using namespace std"???
no..i havent..in fact i've only heard of it..never used it..what exactly does it do?

Sykora
11-08-2006, 11:43 PM
Ah, there's your problem.

Everything you use is defined in what is called a namespace. This is to prevent variable names clashing. All the standard C++ library resources are declared in the std namespace. If you want to use those functions, like cout, cin, etc, you must use the std namespace. otherwise, you must be writing std::cout, std::cin each time, and that's terribly time consuming.

Just insert the statement : using namespace std; somewhere at the top of your document, before you start using anything.

JGuru
11-08-2006, 11:59 PM
It goes like this :

#include <string>
#include <iostream>

using namespace std;

int main()
{
// Write what you need to do here
// ------------
return 0;
}

geekgod
12-08-2006, 12:12 AM
aha..thanx frnds..it worked.
now another problem...it is not working in windows...
giving declaration errors

if this is OT and i should post else where plz tell me so..i'll do that..

mehulved
12-08-2006, 02:05 AM
If you like answers reguarding windows part too in the same thread PM me. I will move it to QnA section where you will get more answers.

geekgod
12-08-2006, 06:09 AM
oh no..its ok thanks..
i just posted here out of laziness in creating a new thread..:-P

and it wont be too problematic if it doesnt work in windows either...i only tried it out coz i cant make my internet work in linux and i have to reboot every time to windows i have to use the net...

thanx for ur helps guys..i guess the mods can close this thread now.