how do I get data from obj 2 while in obj 1?

Hey all

I Have 3 objects (Data, Effects, & Items)

I call Data from my main program and populate.


When Data is called it populate its members using the other 2 objects.

My problem is I need to pull info from a member in Data->Effects to populate a member in Data->Items while i'm in the Items object.
Please show your code what you are trying to do.
hmmm I'll try, it is lots of code to post, so I will only post what is relevant I hope.

MainWindow.ccp --- this is where most of my program runs from.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "stdafx.h"
#include "DataClass.h"

DataClass Data;

WndProc Function(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (message) == WM_CREATE)
		{
		Data.LoadDatafiles()
		return true;
		}

}	


DataClass.h --- header file for DataClass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once

#include "stdafx.h"
#include "EffectClass.h"
#include "ItemClass.h"

class Dataclass
{
public:
	DataClass(void);
	~DataClass(void);

	void LoadDatafiles();
	int GetEffectsIndex(string strEffectName, string strEffectVersion);
private:

	vector <EffectClass> Effects;
	vector <ItemClass> Items;
};

DataClass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "stdafx.h"
#include "DataClass.h"

void DataClass:: LoadDataFiles()
{
LoadEffectsFile();
LoadItemsFile();
}

DataClass:: LoadEffectsFile()
{ 
//gets data from a txt file and load them into the vector Effects
}

int DataClass:: GetEffectsIndex (string strEffectName, string strEffectVersion)
{
	for (unsigned int i=0; i<Effects.size(); i++)
	{
		if (strEffectName == Effect[i].GetName())
		{
			if (strEffectVersion == Effects[i].GetVersion())
				return i;
		}
	}
	return 0;
}

EffectClass.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once

#include "stdafx.h"

class EffectClass
{
public:
	EffectClass(void);
	~EffectClass(void);

	void CreateEffect(string EffectData);
	string GetName();
	string GetVersion();
private:
	string Name;
	string Version;
}

EffectClass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include "EffectClass.h"

void EffectClass::CreateEffect()
{
// this is where I assign the individual effect members
}

string EffectClass::GetName()
{
return Name;
}

string EffectClass::GetVersion()
{
return Version;
}

ItemClass.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#pragma once

#include "stdafx.h"

class ItemClass
{
public:
	ItemClass(void);
	~ItemClass(void);

	void CreateItem(string ItemData)
private:
	string Name
	int ML
	vector <int> Effects;
}

ItemClass.cpp
1
2
3
4
5
6
7
8
9
#include "stdafx.h"
#include "ItemClass.h"

void ItemClass::CreateItem(string ItemData)
{

// this is where i need help at
	Effects.pushback(Data.GetEffectIndex(strEffectName, strEffectVerstion));
}

this section above is where i need help at, how do I reference the Data object to retrieve the effect info that i need?
bumping this, i could really use some help on this.
Last edited on
Hope this example can help you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <vector>

#include <boost/shared_ptr.hpp>

using namespace std;
class Hands
{
	public:
	void Set_Fingers(int fingers);
	void Set_Hands(int hands);
	int  Get_Hands();
	int	 Get_Fingers();

	private:
	int m_fingers;
	int m_hands;
};

int Hands::Get_Hands()
{
	return m_hands;
}

int Hands::Get_Fingers()
{
	return m_fingers;
}
void Hands::Set_Fingers(int fingers)
{
	m_fingers = fingers;
}

void Hands::Set_Hands(int hands)
{
	m_hands = hands;
}
class Body
{
	public:

	int fingers();	
	int hands();
	void attach_hands(boost::shared_ptr<Hands> hand);

	private:
	boost::shared_ptr<Hands> m_hand;
};

void Body::attach_hands(boost::shared_ptr<Hands> hand)
{
	m_hand = hand;
}

int Body::fingers()
{
	return m_hand->Get_Fingers();
}

int Body::hands()
{
	return m_hand->Get_Hands();
}


int main()
{
	// i have a main container class
	Body *body = new Body;
	
	// create an instance of the Hands class
	boost::shared_ptr<Hands> H (new Hands);
	// set parameters
	H->Set_Fingers(10);
	H->Set_Hands(2);
	
	// copy the instance to body
	body->attach_hands(H);

	// i can call the functions of Hands inside the body 
	cout << "I have " << body->hands()   << " hands. " << endl;
	cout << "I have " << body->fingers() << " fingers." << endl;

int x;
cin >> x;
return 0;

}
Sorry, but i don't believe that helps me at all, as you calling both objects from inside main.

I am calling 1 object from Main called Data. Then while in Data calling 2 other object Items & Effects. While i'm in Items i need to retrieve a value from Effects through Data.
I misunderstood your post. I thought you wanted to access some data from another class.

Body = data.

Hands = effect or index.

getting data from another class here.
body->hands();

basically that is what my code is doing.





Pass a reference to a Dataclass object or a EffectClass object to CreateItem()
1
2
3
4
5
void ItemClass::CreateItem(string ItemData, Dataclass Data)
{

	Effects.push_back(Data.GetEffectIndex(strEffectName, strEffectVerstion));
}
i don't know if i really understand you, but i think you want pointers and references. when you construct a Data object, you need to pass a reference to an Effects object and a reference to a Items object.

something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class EffectClass{
  //some effect
};

class ItemClass{
  //something that an item has
};

class DataClass{
  public:
  vector <EffectClass> Effects;
  vector <ItemClass> Items;
  void LoadDataFiles(Effects*, Items*);
}:
void DataClass::LoadDataFiles(EffectsClass* effects_ptr, ItemClass* items_ptr){ //_ptr stands for 'pointer'
//do here whatever you want to effects_ptr, which is a reference to the original Effects object.
}

int main(){
  DataClass data;
  data.LoadDataFiles(&data.Effects[0], &data.Items[0]);
  return 0;
}


hint: people usually name classes ThisWay, functions thisWay and variables this_way. i got lost some times in your code trying to find what was what.
Last edited on
@ maeriden

I remember trying something like this but was very unsuccessful. If i remember correctly, when doing this i had to include DataClass.h in ItemClass.h. when i did this it caused more errors since the 2 classes were essentially doing an infinite loop.

Could you maybe elaborate more on the whole reference thing as it pertains to my code.
when doing this i had to include DataClass.h in ItemClass.h. when i did this it caused more errors since the 2 classes were essentially doing an infinite loop.

That's weird, #pragma once should handle exactly this.

---

A reference is somewhat like a pointer. Passing an argument by reference allows you to access the original object inside the function (it is not copied). You can think of it as an implicitly deferenced pointer. The biggest differences are that a reference is not nullable, and it must be initialized when it is defined (that means in the constructor initialization list if it's a member variable).
If you don't know how to use references I suggest you look for a tutorial as they are a powerful tool and I'm not sure I can explain them properly.

The important thing here is, if you declare a function that uses a reference you don't need to #include any header to make a legal declaration. Forward declaring the type of the reference is enough. Then, in the implementation file (e.g. ItemClass.cpp), you #include the header to access the details of the referenced object. Since the inclusion doesn't happen in any header file you are sure you won't have any recursive inclusion problems.

Forward declaring a type can be done with references and pointers, and indeed should be done wherever possible.
Looks like I finally got this work, here is code that works. thanks to all that helped.

MainWindow.ccp --- this is where most of my program runs from.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "stdafx.h"
#include "DataClass.h"

DataClass Data;

WndProc Function(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (message) == WM_CREATE)
		{
		Data.LoadDatafiles()
		return true;
		}

}	


DataClass.h --- header file for DataClass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once

#include "stdafx.h"
#include "EffectClass.h"
#include "ItemClass.h"

class Dataclass
{
public:
	DataClass(void);
	~DataClass(void);

	void LoadDatafiles();
	int GetEffectsIndex(string strEffectName, string strEffectVersion);
private:

	vector <EffectClass> Effects;
	vector <ItemClass> Items;
};

DataClass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "stdafx.h"
#include "DataClass.h"

void DataClass:: LoadDataFiles()
{
LoadEffectsFile();
LoadItemsFile();
}

DataClass:: LoadEffectsFile()
{ 
//gets data from a txt file and load them into the vector Effects
}

DataClass::LoadItemsFile()
{
	//get data from a txt file then send the info 1 line at time to the ItemsClass.
	Items[Items.size()-1].CreateItem(ItemDataString, *this);
}

int DataClass:: GetEffectsIndex (string strEffectName, string strEffectVersion)
{
	for (unsigned int i=0; i<Effects.size(); i++)
	{
		if (strEffectName == Effect[i].GetName())
		{
			if (strEffectVersion == Effects[i].GetVersion())
				return i;
		}
	}
	return 0;
}

EffectClass.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once

#include "stdafx.h"

class EffectClass
{
public:
	EffectClass(void);
	~EffectClass(void);

	void CreateEffect(string EffectData);
	string GetName();
	string GetVersion();
private:
	string Name;
	string Version;
}

EffectClass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include "EffectClass.h"

void EffectClass::CreateEffect()
{
// this is where I assign the individual effect members
}

string EffectClass::GetName()
{
return Name;
}

string EffectClass::GetVersion()
{
return Version;
}

ItemClass.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once

#include "stdafx.h"

class DataClass;

class ItemClass
{
public:
	ItemClass(void);
	~ItemClass(void);

	void CreateItem(string ItemData, DataClass Data);
private:
	string Name;
	int ML;
	vector <int> Effects;
}

ItemClass.cpp
1
2
3
4
5
6
7
8
9
#include "stdafx.h"
#include "ItemClass.h"

void ItemClass::CreateItem(string ItemData, DataClass Data)
{

// 
	Effects.pushback(Data.GetEffectIndex(strEffectName, strEffectVerstion));
}


@maeriden, I still got a lot to learn with references and forward declaration. I've run the above code and everything seems to be working corrrectly, let me know if you see anything wrong and thanks for your help

In ItemClass.h, Data was supposed to be a reference.
 
void CreateItem(string ItemData, DataClass& Data);


and DataClass.h should have been #included in itemClass.cpp.
Remember that if you pass by value the argument is copied, meaning the construction of another instance of DataClass, and you can't modify the original object

By the way, I didn't know that a forward declaration of a complete object would work like that, I though only pointers and references were ok. But if it compiles I guess it's ok.

Note: it's push_back(), not pushback().
Last edited on

and DataClass.h should have been #included in itemClass.cpp.
Remember that if you pass by value the argument is copied, meaning the construction of another instance of DataClass, and you can't modify the original object

By the way, I didn't know that a forward declaration of a complete object would work like that, I though only pointers and references were ok. But if it compiles I guess it's ok.

Note: it's push_back(), not pushback().


Yeah, not sure why it worked but it does for what I wanted. as for passing the copy that works for me as I only needed to reference the value in Effects and not i want to go messing with and break it again :).
Topic archived. No new replies allowed.