This is my first foray into classes, so apologies if I am being REALLY dumb. I was using a whole load of arrays, but that was getting unweildly, with about a dozen different arrays used to describe one object, and so I thought now was the time to try classes, but my first attempt has not gone well. I have just included what (I hope) is the relevant part of the program above.
The errors I get from the compiler are:
line 13: invalid types 'int[int]' for array subscript
line 21: request for member 'set_base' in 'P1', which is of non-class type 'spec[65]'
My intention with the two lines that have produced errors is:
line 21: having created P1 (an array of 'spec's [0] to [64]) I want to run the function set_base to ...
line 13: make the 'base' data item of each element of the array equal to the number of the element MOD 28 (so base[15] will be 15, base[37] will be 9 {37-28}, etc until the final element base [64] is 8 {64-28-28})
I have written line 13 as I would to allocate the appropriate value to a normal array, and have written line 21 based on the rectangle example in the tutorial on this site [Rectangle rect; rect.set_values;]. From this,
I am guessing that I may have just one error (in line 13) and that, once that is fixed, the problem with line 21 might disappear. I have tried adding 'spec.' and 'spec::' before 'base' in line 13, but this has not helped.
For historical reasons, spec[0] has a special meaning and so an extra item is created in the array.
As the final version will have 6400 rather than 64 items, entering initial states for each data item manually is not going to be practical.
I suspect that my errors are something really basic and that the solution will prevent numerous potential problems in the rest of my program.
Thanks for your help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
class spec
{
int base; // other data elements to be added later
public:
void set_base(int num); // other functions to be added later
spec() {}
};
void spec::set_base(int num)
{
while(num>0)
{
base[num]=num%28; // line13
num--;
}
}
int main()
{
spec P1[65];
P1.set_base(64); // line21
}
|