Sign in to follow this  
Plasmarobo

Pointers invalid!?

Recommended Posts

Argh! I cannot get stupid pointers to custom types to work! can someone please tell me what the problem is? edit: *Cleared* [Edited by - Plasmarobo on April 2, 2006 2:48:34 PM]

Share this post


Link to post
Share on other sites
Edit: sorry, I misread the code. Same principle though.

You're declaring your tItem struct after your other struct, so you're attempting to use it before it knows what the hell it is. Place tItem first, then the other stuff.

Like this:
//Item structure.h
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
//This is the structure of an Item
//Created some time in march
//Austen Higgins-Cassidy

#ifndef _ITEM_H_
#define _ITEM_H_

struct tItem
{
string name;
int type;
int id;
int wieght;
int value;
int para1;
int val1;
int para2;
int val2;
bool key;
tItem();

~tItem();

tItem(const tItem& RHS);

void Save(ostream& fOut);

bool Load(ifstream& fIn);

};

struct tEquipment
{
tItem *head;
tItem *body;
tItem *rarm;
tItem *larm;
tItem *rhand;
tItem *lhand;
tItem *boots;
tItem *acc;
tEquipment();
~tEquipment();
tEquipment(const tEquipment& RHS);

void Save(ostream& fOut);
bool Load(ifstream& fIn);
};

class ItemsData
{
private:
vector<tItem> ItemDataBase;

public:
ItemsData();
~ItemsData();

bool LoadItems(const string& filename);
bool SaveItems(const std::string filename);
tItem GetItem(int id);
void DisplayItems();

void MakeItem(const string& iname, int iwieght, int ival, int type, bool key = false);

};

#endif

Share this post


Link to post
Share on other sites
Oh, I thought that if you did that it would cause the mistake.... quite to the contary I guess, thanks alot.
Edit: Didn't solve everything..... I still have this stuff in limbo....
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 item.cpp -o item.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"

item.cpp:85: error: default argument given for parameter 5 of `void ItemsData::MakeItem(const std::string&, int, int, int, bool)'
item.h:73: error: after previous specification in `void ItemsData::MakeItem(const std::string&, int, int, int, bool)'

item.cpp:200: error: declaration of `tEquipment::tEquipment()' outside of class is not definition
item.cpp:201: error: expected unqualified-id before '{' token
item.cpp:201: error: expected `,' or `;' before '{' token

item.cpp:212: error: declaration of `tEquipment::~tEquipment()' outside of class is not definition
item.cpp:213: error: expected unqualified-id before '{' token
item.cpp:213: error: expected `,' or `;' before '{' token
item.cpp:224: error: declaration of `tEquipment::tEquipment(const tEquipment&)' outside of class is not definition
item.cpp:225: error: expected unqualified-id before '{' token
item.cpp:225: error: expected `,' or `;' before '{' token
item.cpp:237: error: declaration of `void tEquipment::Save(std::ostream&)' outside of class is not definition
item.cpp:238: error: expected unqualified-id before '{' token
item.cpp:238: error: expected `,' or `;' before '{' token
item.cpp:249: error: declaration of `bool tEquipment::Load(std::ifstream&)' outside of class is not definition
item.cpp:250: error: expected unqualified-id before '{' token
item.cpp:250: error: expected `,' or `;' before '{' token

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

Execution terminated

Hfile:

//Item structure .h
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
//This is the structure of an Item
//Created some time in march
//Austen Higgins-Cassidy




#ifndef _ITEM_H_
#define _ITEM_H_


struct tItem
{
string name;
int type;
int id;
int wieght;
int value;
int para1;
int val1;
int para2;
int val2;
bool key;
tItem();

~tItem();

tItem(const tItem& RHS);

void Save(ostream& fOut);

bool Load(ifstream& fIn);

};

struct tEquipment
{
tItem *head;
tItem *body;
tItem *rarm;
tItem *larm;
tItem *rhand;
tItem *lhand;
tItem *boots;
tItem *acc;
tEquipment();
~tEquipment();
tEquipment(const tEquipment& RHS);

void Save(ostream& fOut);
bool Load(ifstream& fIn);
};

class ItemsData
{
private:
vector<tItem> ItemDataBase;

public:
ItemsData();
~ItemsData();

bool LoadItems(const string& filename);
bool SaveItems(const std::string filename);
tItem GetItem(int id);
void DisplayItems();

void MakeItem(const string& iname, int iwieght, int ival, int type, bool key = false);

};




#endif





and the cpp file


//Items cpp
//Expantions for items.h
//Austen Higgins-Cassidy
// 4/1/2006

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
using namespace std;
#include "item.h"




ItemsData::ItemsData()
{
ItemDataBase.clear();
}

ItemsData::~ItemsData()
{
ItemDataBase.clear();
}


// Load items based on a filename and returns true or false of the
// success.
bool ItemsData::LoadItems(const string& filename)
{
ifstream fobj;
fobj.open(filename.c_str());
if(fobj.fail() || !fobj.is_open())
return false;
//clear IDB
ItemDataBase.clear();

bool done = false;
while(!done)
{

tItem temp;
done = !temp.Load(fobj);


if(!done)
{
ItemDataBase.push_back(temp);
string buf;
getline(fobj, buf, '\n');
}
}
fobj.close();
return true;
}

// Save all of the items but keep them in the vector
bool ItemsData::SaveItems(const std::string filename)
{
ofstream fptr;
fptr.open(filename.c_str());
if(fptr.fail() || !fptr.is_open())
return false;


for(size_t x = 0; x < ItemDataBase.size(); x++)
{
ItemDataBase[x].Save(fptr);
}
fptr.close();
return true;
}

void ItemsData::DisplayItems()
{
for(size_t x = 0; x < ItemDataBase.size(); x++)
{
ItemDataBase[x].Save(cout);
cout << endl;
}
}

void ItemsData::MakeItem(const string& iname, int iwieght, int ival, int type, bool key = false)
{
tItem nItem;
nItem.id = 1+(int)ItemDataBase.size();
nItem.name = iname;
nItem.wieght = iwieght;
nItem.value = ival;
nItem.type = type;
nItem.key = key;
ItemDataBase.push_back(nItem);
}


tItem ItemsData::GetItem(int id)
{
for(int l=0; l==ItemDataBase.size(); l++)
{
if(ItemDataBase[l].id = id)
return ItemDataBase[l];
}
return ItemDataBase[0];
}












tItem::tItem()
{
name.clear();
type = 0;
id = -1;
wieght = 0;
value = 0;
para1 = 0;
val1 = 0;
para2 = 0;
val2 = 0;
key = 0;
}


// We make a default destructor due to the C++ rule of 3
tItem::~tItem()
{
// Nothing to do
}

// We make a copy constructor due to the C++ rule of 3
tItem::tItem(const tItem& RHS)
{
name = RHS.name;
type = RHS.type;
while(id <= RHS.id)
{
srand(time(0));
id = rand();
}

wieght = RHS.wieght;
value = RHS.value;
para1 = RHS.para1;
val1 = RHS.val1;
para2 = RHS.para2;
val2 = RHS.val2;
key = RHS.key;
}
// For now, when you are developing your game or whatever, it is
// IMPORTANT to make sure that you know the file being written to
// files is clear, concise, and correct! Because of this, you will
// always want to save your data in text format so you can easily
// verify and validate it. When you take this approach, you can
// easily see if there is a bug in your code or in the data files
void tItem::Save(ostream& fOut)
{
fOut << name << endl;
fOut << type << " ";
fOut << id << " ";
fOut << wieght << " ";
fOut << value << " ";
fOut << para1 << " ";
fOut << val1 << " ";
fOut << para2 << " ";
fOut << val2 << " ";
fOut << key << endl;
}

// Same as what I said for the save function. When you are ready for
// deployment, you can make a new Save/Load function that will work
// in binary format. But for now, use text =)
// I also made this a bool to make sure we can load data from the file
bool tItem::Load(ifstream& fIn)
{
if(!getline(fIn, name))
return false;
fIn >> type;
fIn >> id;
fIn >> wieght;
fIn >> value;
fIn >> para1;
fIn >> val1;
fIn >> para2;
fIn >> val2;
fIn >> key;
return true;
}



tEquipment::tEquipment();
{
head = NULL;
body = NULL;
rarm = NULL;
larm = NULL;
lhand = NULL;
rhand = NULL;
boots = NULL;
acc = NULL;
}

tEquipment::~tEquipment();
{
delete head;
delete body;
delete rarm;
delete larm;
delete lhand;
delete rhand;
delete boots;
delete acc;
}

tEquipment::tEquipment(const tEquipment &RHS);
{
head = RHS.head;
body = RHS.body;
rarm = RHS.rarm;
larm = RHS.larm;
lhand = RHS.lhand;
rhand = RHS.rhand;
boots = RHS.boots;
acc = RHS.acc;

}

void tEquipment::Save(ostream& fOut);
{
fOut << head->id << " ";
fOut << body->id << " ";
fOut << rarm->id << " ";
fOut << larm->id << " ";
fOut << rhand->id << " ";
fOut << lhand->id << " ";
fOut << boots->id<< " ";
fOut << acc->id << endl;
}

bool tEquipment::Load(ifstream& fIn);
{
int idtag;
*head = GetItem(fIn >> idtag);
*body = GetItem(fIn >> idtag);
*rarm = GetItem(fIn >> idtag);
*larm = GetItem(fIn >> idtag);
*rhand = GetItem(fIn >> idtag);
*lhand = GetItem(fIn >> idtag);
*boots = GetItem(fIn >> idtag);
*acc = GetItem(fIn >> idtag);
return true;
}






Ahhh! Why does my compiler hate me!?

Share this post


Link to post
Share on other sites
At first I was going to ask why you were storing pointers rather than whole Item objects within an Equipment struct - then I saw the ItemData class. :) So you're making some kind of registry holding "prototype" items eh? That (look, it even has a name! Although the 'classical' implementation is a bit different) works for its purpose (gives you an effect like 'data-driven types') as long as you understand the limitations (no per-instance data, because you don't have separate instances). Oh, and I do hope you're *not* writing the pointer values direct to file when you 'Save' Equipment (that won't work; the items could be anywhere in memory next time), and *not* deleting pointers when you destruct the Equipment (because the Equipment doesn't 'own' that memory; it's held within the ItemData vector).

Also, you should think about what you're going to do about versioning (i.e. in a later version when the item database changes, and you try to play with old save files). You might also consider developing some kind of 'handle' to use rather than a raw pointer (the struct might contain a pointer to an ItemData plus a 'key' into the vector, for example), and other containers for the "database" (for example, a std::map indexed using the object name, or refining that idea, a std::set, where Items then have comparison defined to sort by name). But now we're getting a little beyond For Beginners territory :)

Share this post


Link to post
Share on other sites
Thanks, but Equipment 'owns' the pointers, and I don't destroy the data, just the pointer to it right? I am writing the deference of the pointer to the file (I hope). And do you mean the keyword 'that' or the Flyweight Pattern? What I am trying to do is create pointers that can be set to the inventory of the character, so I don't have to copy the items to equip them. Does the flyweight thing help with that?

Share this post


Link to post
Share on other sites
Quote:
Original post by Plasmarobo
Oh, I thought that if you did that it would cause the mistake.... quite to the contary I guess, thanks alot.
Edit: Didn't solve everything..... I still have this stuff in limbo....
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 item.cpp -o item.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"

item.cpp:85: error: default argument given for parameter 5 of `void ItemsData::MakeItem(const std::string&, int, int, int, bool)'
item.h:73: error: after previous specification in `void ItemsData::MakeItem(const std::string&, int, int, int, bool)'

item.cpp:200: error: declaration of `tEquipment::tEquipment()' outside of class is not definition
item.cpp:201: error: expected unqualified-id before '{' token
item.cpp:201: error: expected `,' or `;' before '{' token

item.cpp:212: error: declaration of `tEquipment::~tEquipment()' outside of class is not definition
item.cpp:213: error: expected unqualified-id before '{' token
item.cpp:213: error: expected `,' or `;' before '{' token
item.cpp:224: error: declaration of `tEquipment::tEquipment(const tEquipment&)' outside of class is not definition
item.cpp:225: error: expected unqualified-id before '{' token
item.cpp:225: error: expected `,' or `;' before '{' token
item.cpp:237: error: declaration of `void tEquipment::Save(std::ostream&)' outside of class is not definition
item.cpp:238: error: expected unqualified-id before '{' token
item.cpp:238: error: expected `,' or `;' before '{' token
item.cpp:249: error: declaration of `bool tEquipment::Load(std::ifstream&)' outside of class is not definition
item.cpp:250: error: expected unqualified-id before '{' token
item.cpp:250: error: expected `,' or `;' before '{' token

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

Execution terminated

Hfile:
*** Source Snippet Removed ***

and the cpp file

*** Source Snippet Removed ***
Ahhh! Why does my compiler hate me!?


I don't tend to use member functions with structs, but a quick glance shows that you've put ';' at the end of all your tEquipment member function implementation headers. This is gonna confuse the hell out of the compiler, you should remove 'em.

For instance:
tEquipment::tEquipment();
{
head = NULL;
body = NULL;
rarm = NULL;
larm = NULL;
lhand = NULL;
rhand = NULL;
boots = NULL;
acc = NULL;
}


should be
tEquipment::tEquipment()
{
head = NULL;
body = NULL;
rarm = NULL;
larm = NULL;
lhand = NULL;
rhand = NULL;
boots = NULL;
acc = NULL;
}



[Edited by - Andorien on April 2, 2006 3:52:40 PM]

Share this post


Link to post
Share on other sites
Default parameter values must be specified only in .h files. In .cpp files they must not.
So, in .h file:
void MakeItem(const string& iname, int iwieght, int ival, int type, bool key = false);

in .cpp file:
void MakeItem(const string& iname, int iwieght, int ival, int type, bool key)
{
...
}

Share this post


Link to post
Share on other sites
item.cpp:212: error: declaration of `tEquipment::~tEquipment()' outside of class is not definition
item.cpp:213: error: expected unqualified-id before '{' token
item.cpp:213: error: expected `,' or `;' before '{' token
item.cpp:224: error: declaration of `tEquipment::tEquipment(const tEquipment&)' outside of class is not definition
item.cpp:225: error: expected unqualified-id before '{' token
item.cpp:225: error: expected `,' or `;' before '{' token
item.cpp:237: error: declaration of `void tEquipment::Save(std::ostream&)' outside of class is not definition
item.cpp:238: error: expected unqualified-id before '{' token
item.cpp:238: error: expected `,' or `;' before '{' token
item.cpp:249: error: declaration of `bool tEquipment::Load(std::ifstream&)' outside of class is not definition
item.cpp:250: error: expected unqualified-id before '{' token
item.cpp:250: error: expected `,' or `;' before '{' token

I believe these kind of errors mean
instead of using
object.method();
use
object->method(); or (*object).method();
same thing.

I didn't read the whole thing, so I'm just assuming, that's the case for those compile errors.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this