Win32

Started by
5 comments, last by popsoftheyear 15 years, 5 months ago
Why cant I declare string a="Hello" in Visual C++ 6.0???
Advertisement
#include <string>std::string a="hello";


Note that very few people would recommend using VS6 these days. Visual Studio 2008 Express is free and has a more standards-compliant compiler.

I doubt that VS6 is the root of this problem though.
Does Standard C++ count?
// string_getline_sample.cpp// compile with: /EHsc// Illustrates how to use the getline function to read a// line of text from the keyboard.//// Functions:////    getline       Returns a string from the input stream.//////////////////////////////////////////////////////////////////////#pragma warning(disable:4786)#include <string>#include <iostream>using namespace std ;int main(){   string s1;   cout << "Enter a sentence (use <space> as the delimiter): ";   getline(cin,s1, ' ');   cout << "You entered: " << s1 << endl;;}
Still not getting it.
Following error comes:-
Compiling...
DX1.cpp
C:\DX1\DX1.cpp(11) : error C2146: syntax error : missing ';' before identifier 'a'
C:\DX1\DX1.cpp(11) : error C2501: 'String' : missing storage-class or type specifiers
C:\DX1\DX1.cpp(11) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

DX1.exe - 3 error(s), 0 warning(s)
I included the following:-
#include <string.h>

string a="hello";
Quote:Original post by sas1992
I included the following:-
#include <string.h>

string a="hello";

Look closely at EasilyConfused example:

* It's <string>, not <string.h>
* It's std::string, not string

Both are because string is part of STL, the standard template library. STL headers (the files you #include) have no .h ending and the types in STL are declared in the std namespace.
Quote:Original post by sas1992
I included the following:-
#include <string.h>

string a="hello";


Look at the EasilyConfused post. You didnt specify the namespace (std) and you included string.h which is the header for C strings, you must include string which is the header for C++ std::strings. You must choose one of the following:

1.
#include <string>using namespace std;int main(){    string a = "hello";}


2.
#include <string>int main(){    std::string a = "hello";}

.
Quote:Original post by sas1992
C:\DX1\DX1.cpp(11) : error C2501: 'String' : missing storage-class or type specifiers


If you directly copy and pasted the error, than from the looks of it you are using 'String' with a capital 'S' and not 'string' with a lower-case 's'. C++ is case-sensitive. And of course, like others said, make sure you are using namespace 'std' and make sure to include the header file like '#include "string"' (no '.h').

Cheers
-Scott

This topic is closed to new replies.

Advertisement