Compatible Support Forums: c++ question

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

c++ question

#1 User is offline   Neus 

  • journeyman
  • Group: Members
  • Posts: 84
  • Joined: 27-December 03

Posted 02 January 2004 - 04:30 AM

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...
0

#2 User is offline   taeuler 

  • journeyman
  • Group: Members
  • Posts: 94
  • Joined: 30-December 03

Posted 02 January 2004 - 06:28 AM

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.

0

#3 User is offline   Maillion 

  • enthusiast
  • Group: Members
  • Posts: 213
  • Joined: 02-January 04

Posted 02 January 2004 - 12:10 PM

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
0

#4 User is offline   Garfield 

  • stranger
  • Group: Members
  • Posts: 2
  • Joined: 02-January 04

Posted 02 January 2004 - 03:51 PM

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 &#40;&#41;
&#123;
    std&#58;&#58;string mystring;
    mystring = "Hello There!";
    std&#58;&#58;cout << mystring << std&#58;&#58;endl;
    return 0;
&#125;


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
0

#5 User is offline   Neus 

  • journeyman
  • Group: Members
  • Posts: 84
  • Joined: 27-December 03

Posted 02 January 2004 - 04:06 PM

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::
0

#6 User is offline   Neus 

  • journeyman
  • Group: Members
  • Posts: 84
  • Joined: 27-December 03

Posted 02 January 2004 - 04:17 PM

yeh that made it work, thx
0

#7 User is offline   Garfield 

  • stranger
  • Group: Members
  • Posts: 2
  • Joined: 02-January 04

Posted 02 January 2004 - 04:24 PM

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++.
0

#8 User is offline   Maillion 

  • enthusiast
  • Group: Members
  • Posts: 213
  • Joined: 02-January 04

Posted 04 January 2004 - 02:39 AM

Thanks, Garfield, I didn't know this had changed in C++. ;(
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users