ForceRegistry error accessing protected variable

I am working on a physics engine, following the cyclone physics engine source code but a I am having trouble with an
error that is occuring in my overloaded operator== function. It is saying that the information is unaccesible.

ForceRegistry.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "ForceRegistry.h"
#include <algorithm>

void ForceRegistry::add(physicsEntity* body, ForceGenerator *fg)
{
	ForceRegistration registration;         
	registration.body = body;         
	registration.fg = fg;         
	registrations.push_back(registration);
}

bool operator==(ForceRegistry::ForceRegistration const& r1, ForceRegistry::ForceRegistration const& r2) 
//These two force registrations in bold are causing the two errors, but when I build the program there aren't any 
//errors, and the build is successful. Is this just a false error or is there something I am missing here?
{         
	if (r1.body==r2.body && r1.fg==r2.fg)             
		return true;         
	else             
		return false; 
}  

void ForceRegistry::remove(physicsEntity* body, ForceGenerator *fg)
{
	ForceRegistration registration;      
	registration.body = body;      
	registration.fg = fg;      
	
	Registry::iterator it = std::find(registrations.begin(), registrations.end(), registration);      
	if (it != registrations.end())            
	registrations.erase(it); 
}

void ForceRegistry::clear()
{
	registrations.clear();
}

void ForceRegistry::updateForces(real duration)
{
	Registry::iterator i = registrations.begin(); // start at the first registration
	for (; i != registrations.end(); i++) // and iterate through the whole list
	{
		// the updateForce method of the i->fg is called
		i->fg->updateForce(i->body, duration); // with the registered rigid body i->body and frame
		// duration passed in as the parameters
	}
}


ForceRegistry.h
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
#pragma once

#include "physicsEntity.h"
#include "ForceGenerator.h"

class ForceRegistry // holds all the force generators and the particles they apply to
{
protected:
	struct ForceRegistration // one force generator and the particle it applies to
	{
	physicsEntity *body;
	ForceGenerator *fg;
	};
	friend bool operator==(ForceRegistry::ForceRegistration const& r1, ForceRegistry::ForceRegistration const& r2);
	typedef std::vector<ForceRegistration> Registry; // just a convenienceā€¦
	Registry registrations; // holds the list of registrations
public:
	void add(physicsEntity* body, ForceGenerator *fg); // registers force generator to the body specified
	void remove(physicsEntity* body, ForceGenerator *fg); // removes a registration from registry
	void clear(); // clears all registrations (does not delete bodies or force generators, just registration*/
	/* Calls all the force generators to update the forces of their corresponding particles */
	void updateForces(double time); //this is the key to understanding the value of the ForceRegistry

};
try a global scope for the operators and make the global operator a friend.
Last edited on
If I were you I would put the operator == for ForceRegistration inside itself:

ForceRegistry.h
1
2
3
4
5
6
7
struct ForceRegistration // one force generator and the particle it applies to
{
	physicsEntity *body;
	ForceGenerator *fg;

	bool operator == (const ForceRegistration& rhs);
};


ForceRegistry.cpp
1
2
3
4
bool ForceRegistration::operator==(const ForceRegistration& rhs)
{
	return body == rhs.body && fg == rhs.fg;
}


Not forgetting to make sure you also have operator == in both the physicsEntity and ForceGenerator classes
Topic archived. No new replies allowed.