String.h and g++/XCode trouble

Posted:
in Mac Software edited January 2014
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
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

  • Reply 1 of 8
    stoostoo Posts: 1,490member
    Reinstalling the developer tools should work. You don't need the anything extra other than the developer tools to compile and run command line applications under Mac OS X.
  • Reply 2 of 8
    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:



    Code:


    std::string foo;







    or like this:

    Code:


    using namespace std;

    string foo;



  • Reply 3 of 8
    rraburrabu Posts: 264member
    Isn't

    Code:


    #include <string.h>





    supposed to be equivalent (sorta) to

    Code:




    #include <string>

    ...

    using namespace std;







    ???



    For C strings library, isn't it:

    Code:




    #include <cstring>







    or

    Code:




    #include <cstring.h>







    I can't really remember which offhand.
  • Reply 4 of 8
    kecksykecksy Posts: 1,002member
    Quote:

    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:



    Code:


    std::string foo;







    or like this:

    Code:


    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.
  • Reply 5 of 8
    Yeah, it's a relatively new thing. g++ 3.3 started complaining about obsolete headers when I used <vector.h>, then I had the same problem. Using <vector> it couldn't find the type without the "std::".



    Note that if you put a "using namespace std" at the begining of a file you can leave off the "std::" throughout the file.
  • Reply 6 of 8
    kecksykecksy Posts: 1,002member
    Quote:

    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?
  • Reply 7 of 8
    amorphamorph Posts: 7,112member
    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.
  • Reply 8 of 8
    kecksykecksy Posts: 1,002member
    Quote:

    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
Sign In or Register to comment.