Reading Custom structures

Started by
0 comments, last by tuita 18 years, 1 month ago
Okay, I have managed to isolate 2 errors in my header file: In file included from main.cpp:8: item.h: In member function `bool ItemsData::LoadItems()': item.h:90: error: expected primary-expression before "char" item.h:90: error: expected `)' before "char" Here is the file:

//Item structure .h
#include <fstream>
#include <string>
#include <queue>
//This is the structure of an Item


using namespace std;

class ItemsData
{
      protected:

struct item
 {
      int id;
      string name;
      int wieght;
      int value;
      char type;
      int para1;
      int val1;
      int para2;
      int val2;
      bool key;
};

private:
queue<item> ItemDataBase;

public:
       
ItemsData(){LoadItems();}


void MakeItem(string iname, int iwieght, int ival, char type)
{
    item nItem;
    item hItem = ItemDataBase.back();
    nItem.id = hItem.id + 1;
    nItem.name = iname;
    nItem.wieght = iwieght;
    nItem.value = ival;
    nItem.type = type;
    nItem.key = false;
    ItemDataBase.push(nItem);
}

void MakeItem(string iname, int iwieght, int ival, char type, bool key)
{
    item nItem;
    item hItem = ItemDataBase.back();
    nItem.id = hItem.id + 1;
    nItem.name = iname;
    nItem.wieght = iwieght;
    nItem.value = ival;
    nItem.type = type;
    nItem.key = true;
    ItemDataBase.push(nItem);
}

bool SaveItems()
{
    fstream fptr;
    fptr.open("items.idb", ios::binary|ios::out|ios::trunc);
    if(fptr.bad())
    return false;
    item tItem;
    while(!ItemDataBase.empty())
    {
    tItem = ItemDataBase.front();
    fptr.write((char *)(&tItem), sizeof(item));
    ItemDataBase.pop();
    }
    
    fptr.close();
    return true;
     
}
    
bool LoadItems()
{
    fstream fobj;
    fobj.open("items.idb",ios::binary|ios::in);
    if(fobj.bad())
    
    return false;
    while((fobj.eof()==false))
    {
    ItemDataBase.push(fobj.read((char *)(&item), sizeof(item) ));
    }
    fobj.close();
    return true;
    
}
};
[\source]

Will someone please tell me what this means an how to fix it? I just don't understand what I am doing wrong...
___________________________________________________Optimists see the glass as Half FullPessimists See the glass as Half EmptyEngineers See the glass as Twice as big as it needs to be
Advertisement
It's complaining because in your 'read' call you're passing a data-type to it rather than an actual object (i.e. you're passing the struct type 'item'). You need to create an 'item' object to use (i.e. like you did in the write function).

Then the next problem you'll find is that the 'read & push' line itself won't compile. Fstream's read function doesn't actually return the object you want to push onto the stack (I think it returns the number of bytes read or something like that). To make it work, you need to do this operation in two steps (i.e. read call in one line, then push the read object in the next line).

Hope this helps, dude.

This topic is closed to new replies.

Advertisement