String.h and g++/XCode trouble
I'm working on a project for computer science class and want to use the XCode IDE. I've never really used an IDE before; I normally use a text editor and the g++ command to compile my software, so I'm having some trouble in Panther.
My problem is I have a program that uses the string data type defined in the string.h header file. I've included it properly, but for some reason, the compiler, XCode or g++, does not recognize the type string and I get an error. I know I have string.h on my computer. It's in the /usr/include directory on my machine. Are XCode and g++ not finding it, and how do I tell them where to look. I figured the standard
#include <string.h>
syntax would work. Does it have something do with my target? I'm using the default BSD shell tool target, which I assume is the same as a standard unix executable.
I've been able to compile my code using
make
on a remote machine, so it don't see my it's not working on my own computer. I have the BSD subsystem and other tools installed.
My problem is I have a program that uses the string data type defined in the string.h header file. I've included it properly, but for some reason, the compiler, XCode or g++, does not recognize the type string and I get an error. I know I have string.h on my computer. It's in the /usr/include directory on my machine. Are XCode and g++ not finding it, and how do I tell them where to look. I figured the standard
Code:
#include <string.h>
syntax would work. Does it have something do with my target? I'm using the default BSD shell tool target, which I assume is the same as a standard unix executable.
I've been able to compile my code using
Code:
make
on a remote machine, so it don't see my it's not working on my own computer. I have the BSD subsystem and other tools installed.
Comments
#include <string>
string.h gets you the C string functions, not the C++ string type.
Also in your C++ code you need to either declare the string like this:
std::string foo;
or like this:
using namespace std;
string foo;
#include <string.h>
supposed to be equivalent (sorta) to
#include <string>
...
using namespace std;
???
For C strings library, isn't it:
#include <cstring>
or
#include <cstring.h>
I can't really remember which offhand.
Originally posted by davechen
I think for C++ strings you're supposed to do this:
Code:
#include <string>
string.h gets you the C string functions, not the C++ string type.
Also in your C++ code you need to either declare the string like this:
std::string foo;
or like this:
using namespace std;
string foo;
Wow, thanks a lot! That actually worked. My program will compile in xcode after adding std:: in front of all the standard library calls.
It's very weird though. I've never seen this syntax before. Is it new or something because it's not any of my books? Just using #include <iostream.h> and cout used to work, but now I have to use #include <iostream> and std::cout, I guess.
Oh, well. I appreciate your help, since I can now test programs on my home machine. Thanks again.
Note that if you put a "using namespace std" at the begining of a file you can leave off the "std::" throughout the file.
Originally posted by davechen
Note that if you put a "using namespace std" at the begining of a file you can leave off the "std::" throughout the file.
That would be useful. How do you do this?
You just put "using namespace std;" at the top of your C++ file to use the std namespace implicitly (so that you don't have to specify it explicitly with std:. This works for any namespace.
If your C++ text doesn't tell you what a namespace is, you need a new one. It's essentially a way of naming collections of objects and methods so that you can, for example, use or define your own "string" class, and refer unambiguously to your string as "my::string" (if your namespace is "my", which is an unhelpful name...) or "std::string" to refer to the standard C++ class.
Originally posted by Amorph
It looks like some peoples' C++ books are a few years out of date. Consider getting new ones.
You just put "using namespace std;" at the top of your C++ file to use the std namespace implicitly (so that you don't have to specify it explicitly with std:. This works for any namespace.
If your C++ text doesn't tell you what a namespace is, you need a new one. It's essentially a way of naming collections of objects and methods so that you can, for example, use or define your own "string" class, and refer unambiguously to your string as "my::string" (if your namespace is "my", which is an unhelpful name...) or "std::string" to refer to the standard C++ class.
I have one book that covers namespaces, but my text book doesn't. I'll have to talk to my professor about that