cant declare a string variable...

Status
Not open for further replies.

geekgod

Journeyman
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

I see right through you.
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.
 
OP
G

geekgod

Journeyman
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?
 
OP
G

geekgod

Journeyman
@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

I see right through you.
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

Wise Old Owl
It goes like this :
Code:
#include <string>
#include <iostream>

using namespace std;

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

geekgod

Journeyman
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..
 
Last edited:

mehulved

18 Till I Die............
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.
 
OP
G

geekgod

Journeyman
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.
 
Status
Not open for further replies.
Top Bottom