Thread: Having trouble accessing members of a struct

  1. #1
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195

    Having trouble accessing members of a struct

    I want to have multiple file instances open at a given time (probably only 5-10 at time at most). So one way I thought of doing it was to have a structure with the fstream instance and the filename (and any other information). Although, when I try and access the structure to set the contents, it gives me the error message: "m_fileio has not been declared". I haven't programmed in a while, so I'm a little out of touch. The error is quite self-explanatory, I'm just not sure why it says they aren't defined.

    Structure Definition:
    Code:
    struct FILE_STRUCT
    {
           std::fstream m_fileio;
           std::string m_filename;
    };
    Code for setting the members of the struct (p_file is a vector of FILE_STRUCT pointers):
    Code:
    p_file.push_back( new FILE_STRUCT );
         
    ( (p_file.end() )->m_fileio).open(filename.c_str());
    ( p_file.end() )->m_filename = filename;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This seems all weird here, since .end() has never and will never return anything valid to use. But the usual: small, compilable example that shows the difficulty, please.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you want to refer to the last item in a container, use the back() method
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Thanks guys, it turns out the only problem was that I was using the 'end()' method instead of 'back()'. Although since then, a further problem has arised with trying to make a 'CloseAllFiles' function. Here is the code and the corresponding error message I get when I try and compile.

    Code:
    void FILEIO::CloseAllFiles()
    {
         int idx;
         
         for(idx = p_file.size(); idx >= 0; idx--)
         {
                    ( p_file[ p_file.begin() + idx ]->m_fileio).close();
                    delete p_file[ p_file.begin() + idx ];
         }
         
         p_file.clear();
    }
    Error Message:
    Code:
    132 C:\Dev-Cpp\Programs\Development Projects\BOF\fileio.cpp no match for 'operator[]' in
      '((FILEIO*)this)->FILEIO::p_file[(&(((std::vector<FILE_STRUCT*, std::allocator<FILE_STRUCT*> >*)((FILEIO*)this)) + 4u)->
        std::vector<_Tp, _Alloc>::begin [with _Tp = FILE_STRUCT*, _Alloc = std::allocator<FILE_STRUCT*>]())->
        __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator+ 
      [with
        _Iterator = FILE_STRUCT**,
        _Container = std::vector<FILE_STRUCT*, std::allocator<FILE_STRUCT*> >
      ](((const ptrdiff_t&)((const ptrdiff_t*)(&idx))))]'
    It says from the error message that the use of '[]' is the problem. I'm not entirely sure why though.
    Last edited by CornedBee; 10-03-2009 at 08:46 AM. Reason: Break that diagnostic into lines

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You're trying to index the vector with an iterator. That's wrong. You can indext with an index (p_file[idx]) or dereference an iterator (*(p_file.begin() + idx)).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The error message is too long to read.

    However:

    Code:
    p_file[ p_file.begin() + idx ]
    should be exactly the same as

    Code:
    p_file[idx];
    It also seems to me, that again you are trying to access the one-beyond-the-last item (p_file[p_file.size()] is out of range).

    You're trying to index the vector with an iterator.
    Doh.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing a struct within a struct
    By Bladactania in forum C Programming
    Replies: 9
    Last Post: 04-21-2009, 08:06 AM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  4. Accessing structure members
    By foniks munkee in forum C Programming
    Replies: 18
    Last Post: 02-13-2002, 03:06 PM
  5. comparing struct members
    By breed in forum C Programming
    Replies: 4
    Last Post: 11-22-2001, 12:27 PM