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

Started by
7 comments, last by Beowulf_ 21 years, 5 months ago
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?
Advertisement
use:

#include <string>
using namespace std;

void main()
{
string a;

}
use:

#include <string>
using namespace std;

void main()
{
string a;

}
what''s that do? it says "a namespace with this name does not exist".
Note how Aaron used <string> and not <string.h>.

Namespaces should be covered in the first chapters of any good C++ book.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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
Trouble rather the tiger in his lair than the Scholar among his books
quote:
Namespaces should be covered in the first chapters of any good C++ book.


And so should

int main()
{
return 0;
}

And NOT void main!!!!
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...
I am the master of ideas.....If only I could write them down...
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement