string library in gcc 3.0.3?
Kayvan A. Sylvan
kayvan@sylvan.com
Thu Feb 7 08:24:00 GMT 2002
On Thu, Feb 07, 2002 at 10:03:37AM -0600, Ukrit Visitkitjakarn wrote:
> Dear all,
>
> I just have gcc/g++ 3.0.3 installed in my unix box running HPUX11.
> After I installed, I ran a very simple test program named test.cpp using
> standard library, <string> and <stdio.h>.
>
> test.cpp:
> #include <string>
> #include <stdio.h>
> int main() {
> string a;
> a="test";
> printf ( "test message: %s", a.c_str());
> return 0;
> }
>
>
> During the compilation(g++ test.cpp), I got the following error message:
>
> test.cpp: In function `int main()':
> test.cpp:5: `string' undeclared (first use this function)
> test.cpp:5: (Each undeclared identifier is reported only once for each
> function
> it appears in.)
> test.cpp:5: parse error before `;' token
> test.cpp:6: `a' undeclared (first use this function)
>
> To double check, I use -H option to print out the include header file and
> found that <string> is one of the included files.
>
> I never received these error messages in the earlier version of gcc (running
> in HPUX 10.x)
Prior versions of gcc were not quite standard-conforming.
The string type is supposed to live in the ``std'' namespace.
Change your program to the one below:
#include <stdio.h>
#include <string>
using std::string; // import string from std namespace
int main() {
string a;
a="test";
printf ( "test message: %s", a.c_str());
return 0;
}
Although, to get closer to C++ style, you might change your test program
to the following:
#include <iostream>
#include <string>
using std::string; using std::cout; using std::endl;
int main() {
string a("test");
cout << "Test message: " << a << endl;
return 0;
}
Best regards,
---Kayvan
--
Kayvan A. Sylvan | Proud husband of | Father to my kids:
Sylvan Associates, Inc. | Laura Isabella Sylvan | Katherine Yelena (8/8/89)
http://sylvan.com/~kayvan | "crown of her husband" | Robin Gregory (2/28/92)
More information about the Gcc-help
mailing list