Thread: Simple string query

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    36

    Simple string query

    Hi all

    I'm a real newbie to c++ and have the following issue that I was hoping you guys could solve for me.

    I want to use strings end up with error :-( Heres my code

    #include <iostream.h>
    #include <string.h>

    int main(int argc, char* argv[])

    {

    int fred;
    string jas;

    fred = 0;
    cout << "In main\n" << fred;

    return 0;

    }


    When I try and compile this I get error C2065: 'string':undeclared identifier, and this is driving me nuts for something so simple...

    Any ideas what I need to do to overcome this? I've hunted through MSDN and it seems to imply that the header I included is correct so something is obviously up...

    I have noticed that if I only include <string> I can sucessfully declare a string by doing std::string fred.

    Should I always use std::string or should I be able to use the code in my example.

    Thanks for advice!!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Use the newer headers, <string.h> is for functions such as strcmp and strcpy and the like... <string> is for the string objects that you are trying to use. Incidentaly the newer version of <string.h> is called <cstring>. Also use <iostream> instead of <iostream.h> and make sure you have something like using namespace std; after all your #include statements.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        int fred;
        string jas;
    
        fred = 0;
        cout << "In main\n" << fred;
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    36

    Follow up

    Thanks for that, I can get my micky mouse example to work now.

    If I could ask a follow on question :-)

    I went on a c++ course a while back and they showed me to use the include<iostream.h> method.

    Whats the advantage of not putting the .h in the include statments?

    (We never touched on namespaces so I think I had better read the FAQ to understand what these do)

    Ta

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    127
    >Whats the advantage of not putting the .h in the include statments?
    You can take advantage of the latest C++ libraries. Using the current C++ header scheme is also a good idea because some compilers may disallow the old headers by default.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM