Assigning std::strings result in error

This is a discussion on Assigning std::strings result in error within the C++ Programming forums, part of the General Programming Boards category; The following code can be found in one of my functions: // ocv is the beginning of an std::list // ...

  1. #1
    Registered User NixPhoeni's Avatar
    Join Date
    Aug 2001
    Posts
    10

    Angry Assigning std::strings result in error

    The following code can be found in one of my functions:

    // ocv is the beginning of an std::list
    // fin is an ifstream open to a file with data in it
    // n, itemNumber, inRoom, and inArea are of type int
    // str is a char *
    // line and name are of type std::string

    str = new char[150];

    ocv->itemNumber = n;
    fin >> n;
    ocv->inRoom = n;
    fin >> n;
    ocv->inArea = n;

    fin.getline(str, 100);
    fin.getline(str, 100);
    line = str;
    if(line!="NONE") ocv->name = line;

    // ------------------------------------------- //

    I've stepped through this code in the Microsoft Visual C++ Debugger and discovered that in this part of the last line:
    ocv->name = line;
    the compiler experiences an unhandled exception (in the header file XSTRING, _PTR is NULL, which doesn't seem right). line and str both have the string that I expected it to have in it and the code still doesn't work. The problem is that I don't know what to do about this error. I would appreciate any help. Thanks!


    -Joe

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Your code looks ok (unless I've missed something), you'll have to post more. Is ocv an iterator? Also why not use the std::string getline so that you don't have to read into a char[] and then convert.
    zen

  3. #3
    Unregistered
    Guest
    Code:
    fin.getline(str, 100);
    fin.getline(str, 100);
    is this intentional? why would you read a second line into the buffer before using the first? i doubt this has anyting to do with your error though. can you provide the error the compiler gave you in addition to the debugger info?

  4. #4
    Registered User NixPhoeni's Avatar
    Join Date
    Aug 2001
    Posts
    10

    Exclamation Clarifications...

    Yes, ocv is an iterator to the correct location in an std::list. I'm sure it's reading into the list because I checked on itemNumber, inRoom, and inArea while debugging and they were the values in the text file.
    I included fstream.h rather than fstream because occasionally I get errors when trying to close ifstreams and ofstreams when I use fstream. I have never figured this outand I know it better, so I decided to stay away from fstream and stick to fstream.h. I would be pleased if I could use all STL classes, so understanding why an ifstream or ofstream would give an error when closing would help me out.
    As to why I used 2 getline calls, as zen knows (I believe), the '\n' is still in the buffer for the first call (because of the fin >> n; before it), so it is necessary. The following is the function's code. Beware: it may be a horrific from a programmer's perspective.

    // obj_list is of type std::vector < std::list <Object> >
    // ROOM_HASH is a constant (10)
    //-------------- Code follows -----------------//
    void fillObjectList()
    {
    std::ifstream fin("objects.odf");
    std::string line;
    std::list <Object>::iterator ocv, end_ocv;
    int n;
    char *str;
    bool moreObjects = true;

    str = new char[101];

    fin >> n;
    while(moreObjects)
    {
    ocv = obj_list[n % ROOM_HASH].begin();
    end_ocv = obj_list[n % ROOM_HASH].end();

    for(int x=0;x<=obj_list[n%ROOM_HASH].size();x++) ocv++;

    ocv->itemNumber = n;
    fin >> n;
    ocv->inRoom = n;
    fin >> n;
    ocv->inArea = n;
    fin.getline(str, 100);
    fin.getline(str, 100);
    line = str;
    if(line!="NONE") ocv->name = line;

    fin.getline(str, 100);
    line = str;
    if(line!="NONE") ocv->desc = line;

    fin.getline(str, 100);
    line = str;
    if(line!="NONE") ocv->pluralDesc = line;

    fin.getline(str, 100);
    line = str;
    if(line!="NONE") ocv->shortDesc = line;

    fin.getline(str, 100);
    line = str;
    if(line!="NONE") ocv->actDesc = line;

    // add to rooms
    fin.getline(str, 100);
    if(!(fin >> n)) moreObjects = false;
    }
    }

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    for(int x=0;x<=obj_list[n%ROOM_HASH].size();x++) ocv++;

    will walk your iterator past the end of your list so that it no longer refers to a node. For example if your list was 10 nodes long, setting the iterator at the begining, node 1, and then incrementing it 11 times, will leave it refering past list::end() - node 12, and you'll get a runtime error when you overwrite memory you shouldn't.

    If you just want to add an entry to a list, you could initialise a temporary object with the values and then use the list::push_back method to add an entry to the end. Then you don't have to worry about specifying a size for the list or walking an iterator through it to reach the last node.

    Also you should probably try to use fstream instead of fstream.h as the new headers are not always compatable with the new ones.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM

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