I'm having a spot of trouble with structs inside a class.

Hi, I'm trying to create two structs that hold ints and doubles, respectively, and put them both inside a class.

Furthermore, I want a vector of each struct type.

I'm getting all sorts of errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  #ifndef IMAGEVECTOR_STACKER_H
#define IMAGEVECTOR_STACKER_H
#include <string>
#include <vector>

class stacker {
private:
    std::string magic_number;
    int width, height, max_color;

    struct {  // note, I have also tried struct pixel {...
        int pRed, pGreen, pBlue;
    } pixel;

    struct {
        double sRed, sGreen, sBlue;
    } stddev;

    std::vector<pixel> staticImage;
    std::vector<stddev> hdImage;


};


#endif //IMAGEVECTOR_STACKER_H 


Any assistance would be much appreciated.
Thank you.
By saying "struct { int variable; } name;" you're declaring an anonymous struct (a struct without a name), with one instance. In your example, "pixel" and "stddev" are not struct names, but object names.

I believe, with fancy things like decltype, it is possible to have a container of anonymous struct objects, but it's probably easier to just do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Example program
#include <iostream>
#include <string>
#include <vector>

class stacker {
public:
    struct pixel {
        int pRed, pGreen, pBlue;
    };

    struct stddev {
        double sRed, sGreen, sBlue;
    };

    std::vector<pixel> staticImage;
    std::vector<stddev> hdImage;
};


int main()
{
    stacker stack;
    stack.staticImage = { {3, 2, 1}, {4, 5, 6}, {255, 255, 255} };
    stack.hdImage = { {0.3, 0.5, 0.6}, {1.0, 1.0, 0.0}, {0.5, 0.5, 0.5} };
}

Last edited on
Okay. I had tried that originally, but I was using
vector<pixel> sample
in main. That was the issue.

Thanks for clearing that part up for me.

I have to do all private member variables, so my main would be something like...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Example program
#include <iostream>
#include <string>
#include <vector>

class stacker {
private:
    struct pixel {
        int pRed, pGreen, pBlue;
    };

    struct stddev {
        double sRed, sGreen, sBlue;
    };

    std::vector<pixel> staticImage;
    std::vector<stddev> hdImage;
};


int main()
{
    stacker stack;
    stack.someFunction(stack);
}

//.cpp

Stacker::someFunction(/*hmm...*/ pixel &img) { // ???
staticImage.pRed = img.Sred; // ?? 

*edit*
That's a question by the way. heh.

**second edit**
or should ...
Stacker::someFunction(pixel &img)
be
Stacker::someFunction(const stacker &img)
?

Thank you.
Last edited on
As it currently is, staticImage is not a pixel. It is a vector of pixels.
So you can't do staticImage.pRed = my_pixel.pRed;, you'd have to do
staticImage[some_index].pRed = my_pixel.pRed;

jjordan33 wrote:
Stacker::someFunction(pixel &img)
be
Stacker::someFunction(const stacker &img)

I think you have to answer that yourself; if you only need information from a single pixel, then pass that. If you need information from the whole 'stacker' object, then pass that.

Mark it const if the object being passed does not need to be modified.
Last edited on
Okay, that clears up everything.

Thank you.
Topic archived. No new replies allowed.