Including string with header?

This is a discussion on Including string with header? within the C++ Programming forums, part of the General Programming Boards category; Sorry to be a bother or anything with my assult of questions, but currently I am working on a class ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    IL
    Posts
    23

    Including string with header?

    Sorry to be a bother or anything with my assult of questions, but currently I am working on a class to handle files. The problem I currently have (and I'm sure it is something simple I am missing here) is that my header file needs the string class to be defined, and even though I included it, it still says it is not defined.

    Visual Studio gives me this error:
    Code:
    Error C2061: syntax error : identifier 'string'
    And here is the header:
    Code:
    #include <string>
    #pragma once
    
    class File
    {
    public:
    	File();
    	~File();
    	bool Exists();							//Returns true if exists
    
    	string Read();							//Read file, returns contents of file if successful.
    	bool Write(string &sContents);			//Write to end of file, returns true if successful.
    
    	bool Overwrite(string &sContents);		//Completely erases contents of file with given contents, returns true if successful.
    	
    	bool Move(string &sDir);				//Moves the file to another directory, and changes the path stored in the object. Returns true if successful.
    	bool Copy(string &sDir,string &sName);	//Copies the file to another directory. Doesn't change the path stored in the class. Returns true if successful.
    
    	bool Delete();							//Deletes the given file.
    private:
    	string filename;
    };
    As always any help you can offer is to great use to me.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by theJ89
    Sorry to be a bother or anything with my assult of questions, but currently I am working on a class to handle files. The problem I currently have (and I'm sure it is something simple I am missing here) is that my header file needs the string class to be defined, and even though I included it, it still says it is not defined.

    ...
    As always any help you can offer is to great use to me.
    Remove #pragma once, as it's non-portable, and in place of it put: using namespace std;

    - xeddiex

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    std::string

    The string class is part of the std namespace.

    xeddiex, your solution is quick and simple, but not really recommended.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Location
    IL
    Posts
    23
    Hah, I forgot to use the namespace. Thank you.

    Also I might mention that the file should only be included once. Is there any replacement for #pragma once?

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by jafet
    std::string

    The string class is part of the std namespace.

    xeddiex, your solution is quick and simple, but not really recommended.
    Very true, My bad.

    - xeddiex

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by theJ89
    Hah, I forgot to use the namespace. Thank you.

    Also I might mention that the file should only be included once. Is there any replacement for #pragma once?
    The following prevents any file that's including this file from being included again.

    #ifndef HEADER_FILE
    #define HEADER_FILE
    class File {...
    .. other declarations
    #endif

    - xeddiex

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    #ifndef HEADER_FILE
    #define HEADER_FILE

    class File {...
    .. other declarations

    #endif
    "HEADER_FILE" needs to be a unique name, so consider using the file name:

    #ifndef FILE_H
    #define FILE_H

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. using definition from header file without including it
    By amitbern in forum C Programming
    Replies: 7
    Last Post: 08-02-2007, 05:48 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21