Jump to content
  • Advertisement
Sign in to follow this  
yewbie

2d Array of a class (inside a class)

This topic is 2949 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

For some reason I cannot get this to work, I assume its some underlying aspect of class generation I do not understand.

I have a base class CUniverse, in that class I make a 2d array of another class CSector like so

CSector SectorList[UNIVERSESIZEX][UNIVERSESIZEX];

It seems as though creating the class in a array like this does not work...
Anytime I try to access my class via

SectorList[1][1].Whatever = 1;

I get a access violation.

Any insight would be great, or perhaps even a better solotion the CSector class is going to have a lot of functions in it and needs to be unique in a 2d array sort of way.

I really don't want to use a struct to hold data but if I have to go that route I guess that's that.

Thanks in advance for reading!

edit:


class CUniverse
{
public:
private:
CSector SectorList[UNIVERSESIZEX][UNIVERSESIZEX];

};

Share this post


Link to post
Share on other sites
Advertisement
Are you accessing the Sectorlist from inside the CUniverse class? Since the 2D array is declared private, you cannot access it directly from outside the class.

Share this post


Link to post
Share on other sites
First of all thank you for the reply.

I am creating the class like so
CUniverse TestUniverse;

Accessing my class for the sector like so

TestUniverse.SectorList[1][1].Value = 1;

I had a function as part of my universe class to access and return a pointer but since this has not been working I have since removed it and moved the SectorList to public so I can access it directly.

Share this post


Link to post
Share on other sites
Actually if I run the same program outside the debugger its saying Exception unknown software exception 0xc00000fd

if I run it through the debugger it brings me to

test dword ptr [ecx],eax ; ...probe it

In CHKSTK.ASM

I am using VC 6.0 (yeah I know old stuff)

Share this post


Link to post
Share on other sites
Yes as a one dimensional array it works great.

:/ Could it just be my old compiler giving me problems?
I would use VC2010 (what I have at home to try it out) but I am at work and can only use what I have a business license for (VC6)

Any thoughts?

Share this post


Link to post
Share on other sites
Quote:
Original post by BCullis
How about the privacy settings for CSector? Is .Value a public field?


Everything in all classes I have moved to public with the same results.

It just appears that a 2d array of a class does not work!

Share this post


Link to post
Share on other sites
Ah I have a clue to what is causing this....

in my class I had two variables outside of the class scope

const int UNIVERSESIZEX = 1000;
const int UNIVERSESIZEY = 1000;

if I hard code these values into the class instead of using a outside of class variable the 2d array of the class works fine, any thoughts on why this would be?


#pragma once
#include "stdafx.h"
#include <string>
#include <vector>
#include "sector.h"

using namespace std;

const int UNIVERSESIZEX = 1000;
const int UNIVERSESIZEY = 1000;


////////////////////////////////////////////////////////////////////////////////////////////////////
// CUniverse Stores all of our sectors and basic information about our universe
class CUniverse
{
public:
//CSector SectorList[UNIVERSESIZEX][UNIVERSESIZEX]; //does not work
CSector SectorList[1000][1000]; //works
private:


};

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!