Page 1 of 1

[SOLVED]Class access help

Posted: Mon Aug 16, 2010 2:25 pm
by cypher1554R
I've been reading some articles and examples on scene organization and structure.
There is an eample containing something like this:

Code: Select all

class Mesh
{
public:
	Mesh();
	~Mesh();
private:
	Mesh(const Mesh& rhs);
which makes it okay to write a declaration like: Mesh someObject;
but makes it not okay to make: std::vector<Mesh> meshes;
cause when I try:

Code: Select all

Mesh someObject;
someObject.importfromfile(...);
meshes.push_back(someObject);
it gives me: error C2248: 'Mesh::Mesh' : cannot access private member declared in class 'Mesh'
pointing me to line: Mesh(const Mesh& rhs);

I tried moving it to public, but then I just get unresolved external symbol - refering to Mesh(const Mesh& rhs);

It doesn't make any sense. Why would it be fine while private, but unresolved while public.. :(

I don't remember being this confused in a long time..

Re: Class access help

Posted: Mon Aug 16, 2010 2:42 pm
by XianForce
Uhh, I'm just spitballing here but here's what it seems to be from my perspective:

When you make the copy constructor private, it can't be accessed, so copies of that object cannot be made. That's what will cause a compile-time error, because something that doesn't have access to the private members of the Mesh class is trying to access it. When you make it public, I'm quite sure it gives you a linking error, because the function call is now appropriate, but I'm assuming you didn't actually define the copy constructor?

Re: Class access help

Posted: Mon Aug 16, 2010 3:17 pm
by avansc

Re: Class access help

Posted: Mon Aug 16, 2010 4:14 pm
by dandymcgee
Did you try just commenting it out and letting it use the default copy constructor?

Re: Class access help

Posted: Mon Aug 16, 2010 4:28 pm
by cypher1554R
The designer of this tutorial obviously wanted to point out that copying will screw thigs up, and it does. I tried commenting out copy constructor so it uses default, and the application shits itself for some reason. Is there a way I can have a dynamic array like std::vector, but avoid copying when adding new instances of that mesh class?

And please, don't ask me what the hell I'm asking. You will hopefully magically understand what I meant :)

Re: Class access help

Posted: Mon Aug 16, 2010 4:50 pm
by dandymcgee
cypher1554R wrote:The designer of this tutorial obviously wanted to point out that copying will screw thigs up, and it does. I tried commenting out copy constructor so it uses default, and the application shits itself for some reason. Is there a way I can have a dynamic array like std::vector, but avoid copying when adding new instances of that mesh class?

And please, don't ask me what the hell I'm asking. You will hopefully magically understand what I meant :)
Use a pointer to a Mesh object instead of creating a new local instance of the Mesh class? (Just guessing here, don't really have enough info to fully debug your situation)

Re: Class access help

Posted: Mon Aug 16, 2010 4:51 pm
by Scoody
My C++ is getting a bit rusty, but I think it's because you're storing <Mesh>'es. Then it has to run the copy constructor to store it in the vector, but if you used <Mesh*>, it won't instantiate it through the copy constructor. Kinda like you can't declare an array of SomeObject someObjectArray[n]; where the default constructor with no parameters is private or not made by the compiler because you've made one with parameters.
That is, IIRC :)

Re: Class access help

Posted: Mon Aug 16, 2010 5:28 pm
by cypher1554R
The pointer thing worked. Thanks a lot guys, you are a charm.

Was planing on actually sleeping tonight, but circumstances have now changed :)

Re: Class access help

Posted: Mon Aug 16, 2010 5:34 pm
by dandymcgee
cypher1554R wrote:The pointer thing worked. Thanks a lot guys, you are a charm.

Was planing on actually sleeping tonight, but circumstances have now changed :)
Haha, sorry/you're welcome. Good luck.