简体   繁体   English

如何在C ++中返回结构数组?

[英]How to return array of structs in C++?

The error is say : 'mypoint' is not declare in this scope. 错误说:“ mypoint”未在此范围内声明。

my struct is 我的结构是

struct point
{
   int x;
   int y;
};

and my code is : 我的代码是:

struct point lineangle(int x1, int y1, int x2, int y2, int n){
    double angle=360/n, s,c;
    int rotated_x,rotated_y;
    DDA(x1,y1,x2,y2);
    for(int i=0;i<n;i++){
        c = cos(angle*3.14/180);
        s = sin(angle*3.14/180);

        rotated_x= (x1 +((x2-x1)*c-(y2-y1)*s));
        rotated_y= (y1 +((x2-x1)*s+(y2-y1)*c));

        struct point mypoint[]={};

        mypoint[i].x=x1;
        mypoint[i].y=y1;
        mypoint[i+1].x = rotated_x;
        mypoint[i+1].y = rotated_y;
//      DDA(x1,y1,rotated_x,rotated_y);
        x2=rotated_x;
        y2=rotated_y;
    }

    return mypoint;
}

i was declare but it not detected. 我被宣布,但未被发现。

The problem here is that you're declaring mypoint inside of the for loop, so it's out of scope for the return value. 这里的问题是,您在for循环声明了mypoint ,因此它超出了返回值的范围。 Try moving the declaration to before the for loop. 尝试将声明移到for循环之前。

struct point mypoint[]={};
for(int i=0;i<n;i++){
  // ...
}
return mypoint;

Of course, that's not the only problem with your code. 当然,这不是代码的唯一问题。 Honestly, I'm not sure exactly what you're trying to do here, but if you're declaring an array on the stack you also need to provide a length: 老实说,我不确定您要在这里做什么,但是如果您要在堆栈上声明一个数组,则还需要提供一个长度:

struct point mypoint[n + 1]={};
for(int i=0;i<n;i++){
  // ...
}
return mypoint;

And mypoint is an array of struct point , not a single struct point , but you are returning a single struct point . mypointstruct point的数组,而不是单个struct point ,但是您将返回单个struct point Either return the whole array or return the element you want: 返回整个数组或返回所需的元素:

struct point mypoint[n + 1]={};
for(int i=0;i<n;i++){
  // ...
}
return mypoint[0];

The other possibility is that you don't really want mypoint to be an array, in which case you should just declare it as struct point mypoint; 另一种可能性是,您实际上并不希望mypoint是数组,在这种情况下,您应该将其声明为struct point mypoint; (outside the loop). (在循环之外)。 Maybe something like 也许像

struct point lineangle(int x1, int y1, int x2, int y2, int n){
    double angle=360/n, s,c;
    int rotated_x,rotated_y;
    struct point mypoint;
    DDA(x1,y1,x2,y2);
    for(int i=0;i<n;i++){
        c = cos(angle*3.14/180);
        s = sin(angle*3.14/180);

        rotated_x= (x1 +((x2-x1)*c-(y2-y1)*s));
        rotated_y= (y1 +((x2-x1)*s+(y2-y1)*c));

        struct point mypoint[]={};

        mypoint.x = rotated_x;
        mypoint.y = rotated_y;
//      DDA(x1,y1,rotated_x,rotated_y);
        x2=rotated_x;
        y2=rotated_y;
    }

    return mypoint;
}

You are declaring mypoint inside of the loop, it should be before the loop. 您在循环内声明了mypoint,它应该在循环之前。 In addition to this, the array also needs a size, if you have an undetermined size you might need a different datatype like a vector. 除此之外,数组还需要一个大小,如果大小不确定,则可能需要其他数据类型(例如向量)。 Lastly, I think you should only be using point mypoint... instead of struct point mypoint... . 最后,我认为您应该只使用point mypoint...而不是struct point mypoint... What you currently have would create a struct of structs, however even this will not compile since you have already used the name “point” to define your original struct and you cannot use it again. 您当前拥有的将创建一个结构体,但是由于您已经使用名称“ point”来定义原始结构体,并且无法再次使用,因此即使编译也无法编译。

1)add typedef to your declaration 2)your function return an array (pointer) 3) move mypoint declaration outsize the loop and use malloc 1)将typedef添加到声明中2)您的函数返回一个数组(指针)3)将mypoint声明移至循环之外并使用malloc

typedef struct point
{
 int x;
 int y;
};


struct point* lineangle(int x1, int y1, int x2, int y2, int n){
double angle=360/n, s,c;
int rotated_x,rotated_y;
DDA(x1,y1,x2,y2);
int i;
struct point* mypoint = malloc(n*sizeof(struct point));
for(i=0;i<n;i++){
c = cos(angle*3.14/180);
s = sin(angle*3.14/180);
rotated_x= (x1 +((x2-x1)*c-(y2-y1)*s));
rotated_y= (y1 +((x2-x1)*s+(y2-y1)*c));
mypoint[i].x=x1;
mypoint[i].y=y1;
mypoint[i+1].x = rotated_x;
mypoint[i+1].y = rotated_y;
//DDA(x1,y1,rotated_x,rotated_y);
x2=rotated_x;
y2=rotated_y;
}
return mypoint;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM