Archived

This topic is now archived and is closed to further replies.

Beowulf_

#include <string.h> won't work!

Recommended Posts

I want to use the standard string class, but it won''t work for some reason! I''m using visual studio.net, and I create a standard console application. In this application, I write the line: #include <string.h> at the top. Then, in my code, I write the line: string abc; ...and for some reason, the compiler treats ''string'' as just a standard undefined variable, and generates an error. Now, to further confuse matters, when i type, a few lines down: abc. when i type the . a list comes up showing all the members of the string class! It''s really weird. Anyone have any ideas? Is there something really obvious I''m forgetting to do?

Share this post


Link to post
Share on other sites
quote:
Original post by AronBrown
use:

#include <string>
using namespace std;

void main()
{
string a;

}


You don''t need the "use namespace std;" here, and even if you did, it is declared in [tt][/tt], not [tt}[/tt]. HTH.

Trouble rather the tiger in his lair than the Scholar among his books

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:

Namespaces should be covered in the first chapters of any good C++ book.
[/quote ]

And so should

int main()
{
return 0;
}

And NOT void main!!!!

Share this post


Link to post
Share on other sites
In my school, the teacher always said to use void main, which is incredibly stupid. I always use int (unless the project is graded, then I rush to change it into void).

I have never heard of "namespace" though. What is it.


--------------------------------------
I am the master of stories.....
If only I could just write them down...

Share this post


Link to post
Share on other sites
quote:
Original post by Nathaniel Hammen
In my school, the teacher always said to use void main, which is incredibly stupid. I always use int (unless the project is graded, then I rush to change it into void).


Your teacher is wrong. The C++ Standard mandates for int main() and int main(int, char* []). Anything else is implementation-defined, though void main() is explicitely ruled out.

quote:
ISO 98 C++ Standard section 3.6.1 clause 2
An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type [Fruny: the type of the function] is implementation-defined. All implementations shall allow both of the following definitions of main:
int main() { /* ... */ } and int main( int argc, char* argv[]) { /* ... */ }
[Fruny: and so on, about the meaning of argc and argv]



What might have confused him is the following :

quote:
ISO 98 C++ Standard section 3.6.1 clause 5
A return statement in main has the effect of leaving the main function (destroying any object with automatic storage duration) and calling exit with the return value as argument. [Fruny: which means you must return an int.] If control reaches the end of main without encountering a return statement, the efect is that of executing return 0;


So it is not necessary to have a return statement; but if you do have one, it must return an int. main never returns void

quote:

I have never heard of "namespace" though. What is it.



Here''s a link. Ignore their use of <iostream.h>, it is incorrect - the iostream header is <iostream> - ironically, one of the differences is that the classes in <iostream> live in the std namespace ...

In light of what your teacher thinks is proper C++, I urge you to buy a real C++ book : "Essential C++" by S. Lippman, or "Accelerated C++" by A. Koenig and B. Moo - both ed. Addison-Wesley.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites