DIGIT-ARC read-only archive · snapshot: July 23, 2026

cant declare a string variable...

14 posts · originally from https://geek.digit.in/community/threads/cant-declare-a-string-variable.33809/

G
geekgod Original post #1
Aug 11, 2006 at 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
Sykora #2
Aug 11, 2006 at 10:27 AM
I think it is #include <string>, not <strings>
arunks
arunks #3
Aug 11, 2006 at 11:00 AM
yes it is string.h
Sykora
Sykora #4
Aug 11, 2006 at 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
Venom #5
Aug 11, 2006 at 11:36 AM
He's said he tried both...
Sykora
Sykora #6
Aug 11, 2006 at 11:40 AM
He tried "strings", not "string"
G
geekgod #7
Aug 11, 2006 at 4: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
Sykora #8
Aug 11, 2006 at 4:25 PM
Have you included the namespace statement?
G
geekgod #9
Aug 11, 2006 at 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
Sykora #10
Aug 11, 2006 at 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
JGuru #11
Aug 11, 2006 at 11:59 PM
It goes like this :
Code:
#include <string>
#include <iostream>

using namespace std;

int main()
{
       // Write what you need to do here
      // ------------
     return 0;
 }
G
geekgod #12
Aug 12, 2006 at 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..
M
mehulved #13
Aug 12, 2006 at 2: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.
G
geekgod #14
Aug 12, 2006 at 6: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.