Thread: class definition declaring ifstream object

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    class definition declaring ifstream object

    what is wrong with the ifstream declaration here?? i am missing something basic i think, the error is 'ifstream does not name a type'

    Code:
    #ifndef NONSTATE_CLASSES_H_INCLUDED
    #define NONSTATE_CLASSES_H_INCLUDED
    #include <fstream>
    
    class PreviewBoards         //rem, i must create Global object
    {
        private:
    
        SDL_Surface* temp;
        SDL_Surface* Savesource;          //the save preview windows
        SDL_Surface* Minipieces;
        SDL_Rect savewin[6];
        SDL_Rect minipeg[49];
        SDL_Rect miniclip[49];
    
        ifstream Gamefile[6];
    
        int count;
        int dwn;
        int acr;
    
        public:
    
        PreviewBoards(int preview);
        ~PreviewBoards();
    };
    
    
    #endif // NONSTATE_CLASSES_H_INCLUDED

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should qualify the name, i.e., std::ifstream. Incidentally, if you can use a std::istream instead of a std::ifstream, then you might as well do so.

    I note that I/O streams are not copyable, hence objects of your PreviewBoards class will also not be copyable when you have input streams as members.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    .

    You should qualify the name, i.e., std::ifstream
    i did use that and it threw an absolute rack of errors, but if that is the right way then i should check other files for problems.

    I note that I/O streams are not copyable, hence objects of your PreviewBoards class will also not be copyable when you have input streams as members.
    I am better off creating the stream local to the constructor then? the object is only used once to show a set of saved game preview windows, the actual loading and saving uses a different stream elsewhere

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rogster001
    I am better off creating the stream local to the constructor then? the object is only used once to show a set of saved game preview windows, the actual loading and saving uses a different stream elsewhere
    Not necessarily. Do you need the object to be copyable? It sounds like a no, and perhaps some other member variables are not copyable as well. If so, consider disabling the copy constructor and copy assignment operator by declaring them private: it more clearly signals that objects of the class are not copyable, and may also give better error messages if an attempt is made to copy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Not necessarily. Do you need the object to be copyable? It sounds like a no, and perhaps some other member variables are not copyable as well. If so, consider disabling the copy constructor and copy assignment operator by declaring them private:
    this might be the best route then, this object is not copied and it only requires an int arg to be passed in once. no other interaction takes place

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is there a way to tell what class an object is?
    By Loduwijk in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2006, 09:20 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. Replies: 3
    Last Post: 12-03-2001, 01:45 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM