Structs and scruct arrays

问题内容:

Hi! I have a problem with my code here. When I try to access this scruct array i have, it won’t let me access the actual array space i, but rather it gives me the same array values over and over until it’s done. Do any of you see the error? This is a relatively short except out of my code, the names of the functions are correct, don’t worry about that. However, whenever I try to use draw_boxes, it will only ever give me position 0 of boxes[], the first in the array boxes[]; no more, and thereby not giving out what i want. Please keep in mind that I’m an IT student in his first semester, with no previous experience in programming in c++. Something that might be obvious to you isn’t gonna be as obvious to me.

#include <iostream>
#define CIMGGIP_MAIN
#include "CImgGIP05.h"
using namespace std;

struct Box
{
    int x;
    int y;
    int delta_y;
};

const int box_max = 10, box_size = 40;


void draw_boxes(Box boxes[])
{


    white_background();

    for (int i = 0; i < box_max; i++)
    {
        gip_draw_rectangle(boxes[i].x, boxes[i].y, box_size, box_size,
            blue);
        /* don't mind this, it's something we had in our given header file.
        This line is correct*/
    }
}

int main() {

    Box boxes[box_max] = { 0 };



    for (int i = 0; i < box_max; i++)
    {
        boxes[i].x = i * (box_size + 20) + 10;
        boxes[i].y = 0;
        boxes[i].delta_y = random(10, 30);
    }

    draw_boxes(boxes);

    return 0;
}

When I use the function draw_boxes(), i want it to cycle through my array of boxes[], and not just use one.

The result I get is a syntax error. It will only ever use a single space of boxes[], even though I give it the indexes for the other spaces, so it only works with one set of values.

Thanks for the help in advance!

问题评论:

5  
What syntax error to you get? When asking a question about an error, it’s important to share the exact error you are getting.
1  
Your life will be easier if you use std::vector rather than arrays.
    
sadly, i HAVE to use the vector for now. Our professor won’t let us use anything else at the moment
– KidKAt
25 mins ago
1  
@KidKAt So does the program you posted have a syntax error, or does it have the problem of only drawing one box?
2  
@KidKAt If you are relying on the debugger to see what boxes contains in void draw_boxes(Box boxes[]) it will likely only show you the first element. The debugger likely relies on the type of boxes to figure out how to display it to you and has no way to deduce the number of elements boxes contains from it’s type. Please print out to console the positions of the boxes being drawn.

答案:

答案1:

Your code is ok, but your logic is likely wrong.

Your function gip_draw_rectangle is likely:

gip_draw_rectangle(x1,y1,x2,y2,color);

where you want x2 = x1 + width; and y2 = y1 + height;
Your boxes simply overlap. Last one is drawn over all.

try with:

gip_draw_rectangle(boxes[i].x, boxes[i].y, boxes[i].x + box_size, boxes[i].y + box_size, blue);

答案评论:

    
You’re assuming it’s not gip_draw_rectangle(x, y, width, height, color);.
    
yes, exactly. otherwise he’s code goes as expected through all the items of the array.
    
THANK YOU SO MUCH! I guess I just understood the function wrong, since the header file we got isn’t documented at all! This fixed it!
– KidKAt
18 mins ago
    
nice to hear. Then you could validate the answer. Good luck!

答案2:

The problem here was that the function gip_draw_rectangle() was not documented at all, and the boxes were overlapping over and over! What i wrote was gip_draw_rectangle(boxes[i].x, boxes[i].y, box_size,box_size, blue), when in actuality it should’ve been gip_draw_rectangle(boxes[i].x, boxes[i].y, boxes[i].x + box_size, boxes[i].y + box_size, blue), which would be that one adds the length of the box on top of the x and y coordinates. Thanks for the help!

答案评论:

    
Don’t answer your own question. Accept @NicolaeNatea ‘s answer (click the check mark next to his answer).

原文地址:

https://stackoverflow.com/questions/47756077/structs-and-scruct-arrays

Add a Comment