c++ question

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.

Everything Linux 1798 This topic was started by ,


data/avatar/default/avatar30.webp

84 Posts
Location -
Joined 2003-12-27
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...

Participate on our website and join the conversation

You have already an account on our website? Use the link below to login.
Login
Create a new user account. Registration is free and takes only a few seconds.
Register
This topic is archived. New comments cannot be posted and votes cannot be cast.

Responses to this topic


data/avatar/default/avatar04.webp

94 Posts
Location -
Joined 2003-12-30
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.

data/avatar/default/avatar37.webp

213 Posts
Location -
Joined 2004-01-02
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.

data/avatar/default/avatar10.webp

2 Posts
Location -
Joined 2004-01-02
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

data/avatar/default/avatar30.webp

84 Posts
Location -
Joined 2003-12-27
OP
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::

data/avatar/default/avatar30.webp

84 Posts
Location -
Joined 2003-12-27
OP
yeh that made it work, thx

data/avatar/default/avatar10.webp

2 Posts
Location -
Joined 2004-01-02
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++.

data/avatar/default/avatar37.webp

213 Posts
Location -
Joined 2004-01-02
Thanks, Garfield, I didn't know this had changed in C++. ;(