Sign in to follow this  

Overwhelmed by "Friends"

This topic is 4275 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

I am working on creating a function that will load pointers from a list of objects. I am trying to use a friend function to get this, but I am obvisously doing something wrong..... Here are my errors:
ompiler: 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: In member function `bool tEquipment::Load(std::ifstream&)':
item.cpp:257: error: expected primary-expression before '->' token

item.cpp:258: error: expected primary-expression before '->' token
item.cpp:259: error: expected primary-expression before '->' token
item.cpp:260: error: expected primary-expression before '->' token
item.cpp:261: error: expected primary-expression before '->' token

item.cpp:262: error: expected primary-expression before '->' token
item.cpp:263: error: expected primary-expression before '->' token
item.cpp:264: error: expected primary-expression before '->' token
make.exe: *** [item.o] Error 1

Execution terminated

Here is the H file:
//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();
friend struct tEquipment;
        void MakeItem(const string& iname, int iwieght, int ival, int type, bool key = false);

};


 
#endif



and here is the source of the problem (ha, bad pun) 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)
	{
		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)
{
       tItem* itemptr= NULL;
      for(int l=0; l==ItemDataBase.size(); l++)
      {
              if(ItemDataBase[l].id = id)
              {
              *itemptr = ItemDataBase[l];
              return itemptr;
              }
      }
      *itemptr = ItemDataBase[0];
      return itemptr;
}


      




       
      




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 = ItemsData->GetItem(fIn >> idtag);	
		body = ItemsData->GetItem(fIn >> idtag);
		rarm = ItemsData->GetItem(fIn >> idtag);
		larm = ItemsData->GetItem(fIn >> idtag);
		rhand = ItemsData->GetItem(fIn >> idtag);
		lhand = ItemsData->GetItem(fIn >> idtag);
		boots = ItemsData->GetItem(fIn >> idtag);
		acc = ItemsData->GetItem(fIn >> idtag);
		return true;   
       }
       
 





I have been working on this for 1/2 a day, and am at my wits end... I am probably just overlooking something really simple, or I have things in the wrong order.... Could someone please tell me how to arange it? If you want detail about how it will work I can give you those.

Share this post


Link to post
Share on other sites
I'm sorry, but that didn't work.... I'm not sure if it is a good idea to make all my members static. I guess it couldn't hurt, considering that only 1 instance of this class is going to be created, but I just want to access ItemDataBase and GetItem() inside tEquipment.load(). Thanks...

Share this post


Link to post
Share on other sites
Quote:
Original post by Plasmarobo
I'm sorry, but that didn't work.... I'm not sure if it is a good idea to make all my members static. I guess it couldn't hurt, considering that only 1 instance of this class is going to be created, but I just want to access ItemDataBase and GetItem() inside tEquipment.load(). Thanks...


No no no not ALL:
(Example.)

class Main {
private:
static Main *s_pMain;
public:
static Main *Instance() {
return s_pMain;
}
static void InitMain() {
s_pMain = new Main;
}
static void DeinitMain() {
delete s_pMain;
}
void DoStuffWithInstance();
};

int main() {
Main::InitMain();

Main::Instance()->DoStuffWithInstance();

Main::DeinitMain();
}



As you can see, it's not ALL static. Just the stuff that needs to be static. For example, I don't want to have a bunch of Mains so I make some static functions to have only ONE main. But the other things are still per-instance.

In your case, if GetItem doesn't need a specific instance of the class then make it static. If it needs an instance of the class then don't make it static.

Share this post


Link to post
Share on other sites
Here is what MSDN has to say about the friend keyword:

Quote:
From here
In some circumstances, it is more convenient to grant member-level access to functions that are not members of a class or to all functions in a separate class. The friend keyword allows a function or class to gain access to the private and protected members of a class. You can declare friend functions or friend classes to access not only public members but also protected and private class members.


The idea is that if A declares B a friend, then the functions belonging to B can access the private and protected members of an instance of A. Your code in tEquipment::Load is trying to access members of ItemsData, which is not an instance of any class.

Share this post


Link to post
Share on other sites
[lol]
this is funny.
it's like seeing myself from last week.
check out my profile and topics created by me,
and there's something about singletons.
i posted up urls and also working source codes on singletons.
you should be able to use singletons in less than 5 minutes if you breeze through my profile.

hope that helps.

Share this post


Link to post
Share on other sites

This topic is 4275 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.

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