Sign in to follow this  
Jaqen

Need help using array of structs within a class

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[i].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
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_[i].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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this