class function to set array values produces error

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
}
Last edited on
Line 13 - base is treated as an array.
Line 3 - int base; is a single integer variable.

The obvious suggestion it to declare base as an array. But of what size?
In your test, set_base(64) implies that you'd need at least 65 elements.

The array of spec objects spec P1[65]; is playing no real part here, since you'd only use one spec object at a time. For example P1[0]

What are you actually trying to achieve?

This should compile - but whether it does anything useful ...
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
class spec
{
    int base[65]; 

public:
    void set_base(int num); 
    spec() {}
};

void spec::set_base(int num)
{
    if (num >= 65)
        num = 64;
                 
    while (num>0)
    {
        base[num] = num%28; 
        num--;
    }
}

int main()
{
    spec P1;
    P1.set_base(64);
}
Last edited on
This is just initialization, is it not?
Each element of the array will be used later. Read only or modify?
If read only, then why have an array at all? A mere function can return index%28.

Is there any relation between the size of the array and the value that you call the set_base() with?
For example, will you ever set_base(42) the 6400-element array?


You could use std::vector or std::array. They provide syntactic sugar.
Thanks for the replies.

@Chervil
What am I actually trying to achieve?
It is part of a population-dynamics project, looking at gene-flow within the population. 'spec' is short for specimen and the plan was to have class spec containing data-elements representing qualities of each specimen within the population at a given moment in time. So there would be a data-element for 'age of specimen', one for 'reproductive availability', etc. An array of the class would give me one element of the array for each of the 64 (or 6400) specimens. I originally conceived this as a series of arrays, mostly ints with some bools in the mix as well. These were all defined in main(), but this involved passing a lot of arrays into functions, and so I thought that one array of a class might be easier than lots of individual arrays.

Looking at the code, I can see that I could define the class as having data-members that are all arrays (rather than having one array of the class). That might open up some possibilities ... I will go away and play some more.

@keskiverto
Yes, this is just initialization. In the case of this data-element, it won't be modified and I had not considered your function idea. In this instance I guess it would be a lot faster with large number of specimens, when I am only accessing a small number of them at any one time.

With other data-elements, they would need to be modified. For example, with the 'age' data-element I mentioned above: I would initialize to an age distribution graph (x specimens to age1, y specimens to age2, etc) and then these figures would need to increment as the program progresses through time. I was thinking initialise the entire array at the start (which was to be sized to match the final size of the population in the model) with a series of set_???() functions. But your mention of using std::vector, with its dynamic-size properties, might be a better option, allowing me to add/remove specimens as individuals are born/die. I will have to go and read up on vectors ...

Thanks.
Topic archived. No new replies allowed.