Jump to content
  • Advertisement
Sign in to follow this  
Jaqen

Need help using array of structs within a class

This topic is 3688 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

I'm trying to make the constructor create an array.
        struct index { 
            int x;
            int y;
            char object;
        }; // symbols[254], colors[15]

So when the constructor is called, it'll make colors[15], for example. In the constructor I used:
index colors[15];

And it compiles up to that point, finally. But after that I try to assign it a value in a method:
colors.x = (x + (i * 2));

'class Tools' has no member named 'colors' 'colors' undeclared (first use this function) So... does anyone have any idea what I'm doing wrong? It seems to work fine if I declare the array in the struct like so:
        struct index { 
            int x;
            int y;
            char object;
        } colors[15];

but that's not what I want. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Judging from your constructor code, it seems you're simply creating a local array of 15 index structs, which is destroyed as soon as the constructor exits. It also seems you're mixing the common typedef syntax commonly used with structs in C with declearing data members.

Is something like this what you're shooting for?

struct index {
int x, y;
char object;
};
class tools {
public:
...
void foo() {
int x = ..., i = ...;
...
colors_.x = (x + (i * 2)); // Use the array 'colors_'.
...
}
...
private:
index colors_[15];
};

I'm not sure I understand your question, but I hope this is helpful. The tools class has a simple data member which is an array of 15 index structs.

Share this post


Link to post
Share on other sites
If I understand what you're asking, then you should consider using a std::vector<index> in your class. Then you can use the member initialization list to set the size. Ex:

class Tools {
public:
Tools() : colors(15) {}

std::vector<index> colors;
};

Share this post


Link to post
Share on other sites
Is this what you're after?:

class Tools
{
public:
// no constructor needed

void f(int x, int i) { colors.x = x + (i * 2); }

private:
index colors[15];
};

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
If I understand what you're asking, then you should consider using a std::vector<index> in your class.

Good advice. Should've mentioned that. [totally] If you're not comfortable with arrays yet, I suggest reading and practicing with them until they become more familiar, but there's no reason not to take a look at the std::vector container right away either; it behaves much like an array but is safer, providing bounds checking, automatic resizing, copy semantics, etc..

Share this post


Link to post
Share on other sites
To put it simply, I just want to be able to declare an array of my structure in my constructor.

Oh, I think I was thinking about it wrong. I might have it now... I have to have it setup as a private member of the class and then the constructor can initialize x/y/object. That's why I wasn't thinking straight... I thought I needed to init the struct.

dmatter's example made it click for me. Thanks all...

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!