Referencing an Array in Parameter

Hello, I am trying to reference an array in my parameters for my draw functions below however, after looking at other forms I still can't seem to get it right. I still get undeclared errors when trying to reference my objects[]array. Below for each draw function I tried a few different ways that didn't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  void Sketch::draw()
{
	for (int i = 0; i < n; i++)//start with objects[0]
	{
		int c = objects[i].colortype;
		glColor3f(colormenu[c][R], colormenu[c][G], colormenu[c][B]);

		 if (objects[i].tooltype == 0 || 1)
		 	{
				drawRectangle(short int [], int [], int [], int [], int []); //Draw Rec, 5 parameters
			}
		 else
			if (objects[i].tooltype == 2 || 3)
			{
				drawCircle(t, x1, y1, r objects[]);//Draw Circle. 4 parameters
			}
		 else
			if (objects[i].tooltype == 4 || 5)
			{
				drawLine(short int t, int x1, int y1, int x2, int y2 objects[]);//Draw Line, 5 parameters
			}
	 }
}
Last edited on
If you're getting errors saying that objects is an undeclared identifier, it might help to show where you declare and/or define it.

Btw, lines 10, 15 and 20 are incorrect. You don't supply types in function calls, you supply variable names or values. The way we indicate we are tendering an array to a function is to supply the array name or a pointer to the first element. No []'s.

Also, lines 8 13 and 18 are incorrect. All of those conditions will always be true.

if (objects[i].tooltype == 0 || objects[i].tooltype == 1) would be the correct way to write line 8.
Last edited on
@cire oh that make sense.
When supplying variable names are they the same ones as the ones used when declaring the function?
For example:
Here is where I am calling my drawRectangle function from the header file in my .cpp file
 
void Sketch::drawRectangle(short int t, int x1, int y1, int x2, int y2) 

Unlike the functions above entering the same parameters it has in the header file work fine but since I am declaring the function here its okay but, in the ones above I have to reference the parameters, is that correct?

@keskiverto Thank you the links they are very helpful

Thank you both for your help :)
Last edited on
When supplying variable names are they the same ones as the ones used when declaring the function?

They should be the ones you want to feed to the function. The variable names you used when declaring the function are immaterial.
@cire I see. Thank you for your help on the matter! :)
After reading up on some other things I found that when referencing an array there is no need to use 'int' etc.

 
drawCircle(short int objects[], int objects[]);

^ with this I get error:expected primary-expression before ‘short’;

However, when I take out the 'int' and 'short int' I get the same error. In other forums I've read this seem to have solve the issue others have I am not sure why it failed for me, any thoughts?

Thanks :)
However, when I take out the 'int' and 'short int' I get the same error.

So, whenever you remove 'short int' from the code, it still complains about expecting a primary expression before 'short'? That doesn't make a whole lot of sense.

You haven't supplied the context this snippet of code resides in, which would be helpful. Is this a function declaration? Are you attempting to call the function? It's hard to tell because neither one is done correctly, and I have a hard time imagining when it would be appropriate to feed this function the same array twice in the same call.

Turns out I had to pull from my array.

Thank you for the help. :)
Topic archived. No new replies allowed.