Jump to content
Compatible Support Forums
Sign in to follow this  
Neus

c++ question

Recommended Posts

ok, im a newb to c++, and currently teaching it to myself, i noticed one header or whatever file that didnt seem to be there and make my code work...

in the book it says

Quote:

#include <iostream.h>

#include <stdlib.h>

#include <string>

 

int main(int argc, char *argv[])

{

string mystring;

mystring = "Hello there";

cout << mystring << endl;

return 0;

}

 

I did this, but it wont run... i also tried adding string.h instead of just string, and i tried doing the "string" instead of using the <> and same again with/without the .h .... the book is using Dev3++ though, and im using KDevelop.. but any clue of what it is? the errors i get are

 

Quote:

main.cpp:17:error:'string' undeclared (first use this function)

main.cpp:17:error:(each undeclared identifier is reproted only once for each function it appears in.)

main.cpp:17:error:syntax error before ';' token

main.cpp:18:error:'mystring' undeclared (first use this function)

main.cpp:21:4: warning: no newline at end of file

any clue??? as for the last error message, thats always happened when theres errors, and once the other errors are gone it dissapears...

Share this post


Link to post

One possible solution is to use a character array instead of what you have quoted above. Your code would look something like this:

Quote:
char mystring[20] = "Hello there';

cout << mystring << endl;

 

If you only want to output the code, not store it in a variable you would only need to do; cout << "Hello there";.

The header file shouldn't be the problem.

Also, did you get kDevelop off your Mandrake CD's, or another source, over the courese of upgrading my Suse distribution my copy was deleted and I haven't to reinstall it off the old install disks.

Share this post


Link to post

The main() function does not need a declaration, nor any arguments. Everything else needs to be declared. You declared the main() function as a int, strings are not integers, even in C. You really need to go back and reread some of the first few chapters of a good C book. smile

Share this post


Link to post

I see several problems:

 

1. <iostream.h> is not part of standard C++. The 1998 C++ standard uses <iostream> (no ".h")

 

2. Both <iostream> and <string> are in the "std" namespace. To use anything in the standard namespace, you need to append "std::" before the function you are using (example std::cout instead of just cout)

 

Try:

 

Code:
#include <iostream>#include <string>int main (){    std::string mystring;    mystring = "Hello There!";    std::cout << mystring << std::endl;    return 0;}

 

Also, int main() is correct. According to the c++ standard main returns an integer. If you omit the "return 0" statement, the compiler will add it for you.

 

I'd suggest getting a couple of good c++ books. "Accelerated c++" is a good one to start with. Try www.accu.org for book reviews.

 

Hope this helps,

Scott

Share this post


Link to post

oh... well ill try that out, but all the other codes seemed to have still worked ont his compiler too with .h and without std::

Share this post


Link to post

Most compilers still accept the old style ".h" header files for backwards compatibility. These old header files did not use namespaces, so they do not need the "std::" qualifier to be recognized by the compiler. The newer headers in the C++ standard do require this. Most books nowadays should use the newer header files. Those that don't should be, since many things have changed between the pre/post standards of C++.

Share this post


Link to post

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×