Jump to content
  • Advertisement
Sign in to follow this  
Plasmarobo

Last resort!

This topic is 4481 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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. 

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
(char *)(&item)

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

IOW

item theItem;
.
.
.
(char *)(&theItem)

Share this post


Link to post
Share on other sites
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 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: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 1

Execution terminated

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Um... you put your item declaration inside an if statement, and to further complicate things you've taken the return false out of it!!!!


Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!