[SOLVED]Class access help

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
cypher1554R
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1124
Joined: Sun Jun 22, 2008 5:06 pm

[SOLVED]Class access help

Post 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..
Last edited by cypher1554R on Mon Aug 16, 2010 5:28 pm, edited 1 time in total.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Class access help

Post 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?
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Class access help

Post by avansc »

Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Class access help

Post by dandymcgee »

Did you try just commenting it out and letting it use the default copy constructor?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
cypher1554R
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1124
Joined: Sun Jun 22, 2008 5:06 pm

Re: Class access help

Post 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 :)
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Class access help

Post 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)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: Class access help

Post 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 :)
User avatar
cypher1554R
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1124
Joined: Sun Jun 22, 2008 5:06 pm

Re: Class access help

Post 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 :)
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Class access help

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply