Class within a function

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<iostream>
#include<string.h>

using namespace std;

class color
{
public:
    int p,n,ne = 0;
    int R,G,B;

int set_color(int x,int y,int z)
    {
    x = R;
    y = G;
    z = B;
    }


color();

color(int p, int n, int ne)
    {
    if(p >= 255)
    {
        p = 255;
    }
    else(p = p);

    if(n >= 255)
    {
        n = 255;
    }
    else(n = n);

    if(ne >= 255)
    {
        ne = 255;
    }
    else(ne = ne);
    }

int add(int a,int b,int c)
{
    R = max(R,a);
    G = max(G,b);
    B = max(B,c);
}

int mix(int a,int b,int c)
    {
    R = (R + a) /2 ;
    G = (G + b) /2 ;
    B = (B + c) /2 ;
    }

};








int main()
{




    const color Black(0,0,0);
    const color White(255,255,255);
    const color SkyBlue(135,206,235);
    const color SeaGreen(46,139,87);
    const color Gold(255,215,0);
    const color Chocolate(210,105,30);
    const color DarkGrey(75,75,75);

    color Custom(White);
    Custom.set_color(DarkGrey);
    Custom.mix(DarkGrey);
    Custom.mix(SkyBlue);

    color CreamyMix(White);
    CreamyMix.mix(SkyBlue);
    CreamyMix.mix(SeaGreen);
    CreamyMix.mix(White);
    CreamyMix.mix(White);

    color mixes[5];

    mixes[0] = SkyBlue;
    mixes[0].add(SeaGreen);
    mixes[0].mix(Black);

    mixes[1].set(Chocolate.B(),SeaGreen.R(),DarkGrey.G());
    mixes[1].mix(Gold);

    mixes[2] = White;
    mixes[2].add(Black);
    mixes[2].add(Gold);

    mixes[3].set(Chocolate.R(),Gold.G(),SeaGreen.B());
    mixes[3].add(SkyBlue);

    mixes[4] = mixes[3];
    mixes[4].mix(Black);
    mixes[4].mix(Black);

    return Black.R();


};



I've been trying to figure out how to execute the 82nd line of code ( Custom.mix(DarkGrey);) and i'm not really sure how to do this. So I understand everything up to this point, but I'm not really sure how to put a class value into a function. So if I understand this (correct me if i'm wrong) the Custom class is declared with the RGB values of "White". The .mix function is supposed to take the "DarkGrey" values and run them through with Custom class RGB values. This is where I keep getting an error message telling me that I must input three not just one value into the .mix function. The Color Class I wrote, so it may have errors, but the rest of the program is given. Any help understanding this would be greatly appreciated.

Thanks.
Last edited on
Make your functions void, because they do not return anything. Also return 0 on main instead of Black.R() even though it has the same result.
1
2
3
4
5
void mix(color colour){
  R = (R + colour.R)/2;
  G = (G + colour.G)/2;
  B = (B + colour.B)/2;
}
A class is a type, like any other. So when you're defining your method, just declare the argument type to be the class that you want to pass in.

I really recommend against making the class's data members public, by the way. That just screws up encapsulation.
Topic archived. No new replies allowed.