Passing an object to another class

Started by
16 comments, last by Honas 12 years, 1 month ago
mCamera is a reference, not a pointer. You don't use -> to access the members of a reference.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Advertisement
Thanks, but these errors remain:
1>------ Build started: Project: TheGame, Configuration: Debug Win32 ------
1> Terrain.cpp
1>c:\-\thegame\terrain.cpp(121): error C2327: 'Terrain::mCamera' : is not a type name, static, or enumerator
1>c:-\\thegame\terrain.cpp(121): error C2065: 'mCamera' : undeclared identifier
1>c:\-\thegame\terrain.cpp(121): error C2228: left of '.getPos' must have class/struct/union
1> type is ''unknown-type''
1>c:\-\thegame\terrain.cpp(121): error C2228: left of '.vD3DVector' must have class/struct/union
1>c:\-\thegame\terrain.cpp(122): error C2327: 'Terrain::mCamera' : is not a type name, static, or enumerator
1>c:\-\thegame\terrain.cpp(122): error C2065: 'mCamera' : undeclared identifier
1>c:\-\thegame\terrain.cpp(122): error C2228: left of '.getPos' must have class/struct/union
1> type is ''unknown-type''
1>c:\-\thegame\terrain.cpp(122): error C2228: left of '.vD3DVector' must have class/struct/union
Look up the error code, it tells you EXACTLY what's wrong.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.


Look up the error code, it tells you EXACTLY what's wrong.


I've looked into it before and I am still clueless.
SubGrid does not know mCamera. Using Terrain::mCamera does not work. Making mCamera static also. :/ Thanks for your help, though.
It says: "Camera & Terrain::mCamera Error: a nonstatic member reference must be relative to a specific object"
But this gets called after the constructor where I pass the specific object (the reference) to mCamera. I'm clueless.
I would highly recommend learning the language before you start too far down the road with your game. Especially when your solution to problems is "damn, I don't understand why this is illegal, I'll just make it a global."

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

The reason my problem actually exists is because I do not want to use my camera class globally, like in Luna's initial code example.
Your SubGrid type is an inner class of Terrain. However, it has no relationship with the parent class. That is, a SubGrid isn't associated with any particular Terrain instance. One solution is to put a pointer to Terrain in the SubGrid (intiialised in the constructor). The operator<() can use this pointer to access the camera.

As Washu correctly points out - this is a basic language issue.


Well I took out everything that is not part of the problem.
[/quote]
No, there is still a huge amount of code in that that is nothing to do with the problem. This is what the code would look like if you had taken everything out:


#ifndef CAMERA_H
#define CAMERA_H

class Camera
{
bool example;
};

#endif

#ifndef TERRAIN_H
#define TERRAIN_H

class Terrain
{
public:
Terrain(Camera &camera);

class SubGrid
{
bool operator<(const SubGrid &other) const;
};
private:
Camera &camera;
};

#endif

#include "terrain.h"

Terrain::Terrain(Camera &camera) : camera(camera)
{
}

bool Terrain::SubGrid::operator<(const SubGrid &other) const
{
return camera.example;
}

int main()
{

}

This code distils the essence of your problem - it should generate exactly the error you were looking at.

Also note that the code dump you gave is not properly indented, which makes it very difficult to decipher. Once again, having less code would make this less of an issue.

As an aside, it is usually a good idea to not use references as members. References have strange semantics as members because they are not reseatable. Instead, convert the reference to a pointer. The constructor can still take a reference, which neatly documents the intent of the class.

Your SubGrid type is an inner class of Terrain. However, it has no relationship with the parent class. That is, a SubGrid isn't associated with any particular Terrain instance. One solution is to put a pointer to Terrain in the SubGrid (intiialised in the constructor). The operator<() can use this pointer to access the camera.

As Washu correctly points out - this is a basic language issue.


Well I took out everything that is not part of the problem.

No, there is still a huge amount of code in that that is nothing to do with the problem. This is what the code would look like if you had taken everything out:


#ifndef CAMERA_H
#define CAMERA_H

class Camera
{
bool example;
};

#endif

#ifndef TERRAIN_H
#define TERRAIN_H

class Terrain
{
public:
Terrain(Camera &camera);

class SubGrid
{
bool operator<(const SubGrid &other) const;
};
private:
Camera &camera;
};

#endif

#include "terrain.h"

Terrain::Terrain(Camera &camera) : camera(camera)
{
}

bool Terrain::SubGrid::operator<(const SubGrid &other) const
{
return camera.example;
}

int main()
{

}

This code distils the essence of your problem - it should generate exactly the error you were looking at.

Also note that the code dump you gave is not properly indented, which makes it very difficult to decipher. Once again, having less code would make this less of an issue.

As an aside, it is usually a good idea to not use references as members. References have strange semantics as members because they are not reseatable. Instead, convert the reference to a pointer. The constructor can still take a reference, which neatly documents the intent of the class.
[/quote]

Thank you!

This topic is closed to new replies.

Advertisement