Last resort!

Started by
11 comments, last by Drew_Benton 18 years, 1 month ago
This is my last resort before I just give it up. What does this mean and how do I fix it? Should I use pointers?

//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]

Sorry, I have already posted in for beginners, and noone knew what it was or suggested a solution. Feel free to ridicule, but my feelings bruse easily. 

___________________________________________________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
What is broken about it?

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Sorry, here is the error list:
Executing make...
make.exe -f "C:\Dev-Cpp\projects\1\Makefile.win" all
g++.exe -c main.cpp -o main.o -I"lib/gcc/mingw32/3.4.2/include" -I"include/c++/3.4.2/backward" -I"include/c++/3.4.2/mingw32" -I"include/c++/3.4.2" -I"include"

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"

make.exe: *** [main.o] Error 1

Execution terminated
___________________________________________________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
(char *)(&item)

Shouldn't you be using a variable of type "item" instead of "&item"?

IOW

item theItem;
.
.
.
(char *)(&theItem)
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
Okay it is now like this:

bool LoadItems(){    fstream fobj;    fobj.open("items.idb",ios::binary|ios::in);    if(fobj.bad())    item myItem;    return false;    while((fobj.eof()==false))    {    ItemDataBase.push(fobj.read((char *)(&myItem), sizeof(item) ));    }    fobj.close();    return true;    }};[\source]Compiler: Default compilerBuilding Makefile: "C:\Dev-Cpp\projects\1\Makefile.win"Executing  make...make.exe -f "C:\Dev-Cpp\projects\1\Makefile.win" allg++.exe -c main.cpp -o main.o -I"lib/gcc/mingw32/3.4.2/include"  -I"include/c++/3.4.2/backward"  -I"include/c++/3.4.2/mingw32"  -I"include/c++/3.4.2"  -I"include"   In file included from main.cpp:8:item.h: In member function `bool ItemsData::LoadItems()':item.h:90: error: `myItem' undeclared (first use this function)item.h:90: error: (Each undeclared identifier is reported only once for each function it appears in.)make.exe: *** [main.o] Error 1Execution terminated
___________________________________________________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
Um... you put your item declaration inside an if statement, and to further complicate things you've taken the return false out of it!!!!


Okay, thanks for catching that: New error:

Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\projects\1\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\projects\1\Makefile.win" all
g++.exe -c main.cpp -o main.o -I"lib/gcc/mingw32/3.4.2/include" -I"include/c++/3.4.2/backward" -I"include/c++/3.4.2/mingw32" -I"include/c++/3.4.2" -I"include"

In file included from main.cpp:8:
item.h: In member function `bool ItemsData::LoadItems()':
item.h:91: error: no matching function for call to `std::queue<ItemsData::item, std::deque<ItemsData::item, std::allocator<ItemsData::item> > >::push(std::basic_istream<char, std::char_traits<char> >&)'
C:/Dev-Cpp/Bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_queue.h:215: note: candidates are: void std::queue<_Tp, _Sequence>::push(const typename _Sequence::value_type&) [with _Tp = ItemsData::item, _Sequence = std::deque<ItemsData::item, std::allocator<ItemsData::item> >]

make.exe: *** [main.o] Error 1

Execution terminated



code:
bool LoadItems(){    fstream fobj;    fobj.open("items.idb",ios::binary|ios::in);    item myItem;        if(fobj.bad())    return false;    while((fobj.eof()==false))    {    ItemDataBase.push(fobj.read((char *)(&myItem), sizeof(item)));    }    fobj.close();    return true;    }[\source]
___________________________________________________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
Read does not return an item. Trying to push() a non item into a container of items, thus, does not work.

ItemDataBase.push(fobj.read((char *)(&myItem), sizeof(item)));

Should become:

fobj.read((char *)(&myItem), sizeof(item));
ItemDataBase.push(myItem);
Sorry: How did he get a "[\source]" at the end of the code block, without terminating the code block at that point? :)
--== discman1028 ==--
Quote:Original post by discman1028
Sorry: How did he get a "[\source]" at the end of the code block, without terminating the code block at that point? :)


The terminating tag is [/source] not [\source].

This topic is closed to new replies.

Advertisement