【问题标题】:How to return array of structs in C++?如何在 C++ 中返回结构数组?
【发布时间】:2020-03-23 13:39:15
【问题描述】:

错误是说: 'mypoint' 未在此范围内声明。

我的结构是

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);
    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;
}

我已声明但未检测到。

【问题讨论】:

  • 选择 C ​​标签或 C++ 标签。
  • 这能回答你的问题吗? C++ Return Array of Structs
  • @VladfromMoscow 你为什么把它重新标记为 C?标题是 C++。
  • @Chipster 因为他总是使用带有结构类型标识符的关键字 struct。好像是C代码。
  • @VladfromMoscow 虽然这看起来确实像 C,但可能是因为前 C 程序员正在使用 C++ 编程。不幸的是,我们现在只需要继续标题。

标签: c++ arrays struct


【解决方案1】:

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;
}

【讨论】:

    【解决方案2】:

    这里的问题是你在 for 循环中声明了 mypoint inside,所以它超出了返回值的范围。尝试将声明移到 for 循环之前。

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

    当然,这不是您的代码的唯一问题。老实说,我不确定你到底想在这里做什么,但如果你在堆栈上声明一个数组,你还需要提供一个长度:

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

    mypointstruct point 的数组,而不是单个struct point,但您返回的是单个struct point。要么返回整个数组,要么返回你想要的元素:

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

    另一种可能性是您并不真正希望 mypoint 成为一个数组,在这种情况下,您应该将其声明为 struct point mypoint;(在循环之外)。也许像

    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;
    }
    

    【讨论】:

    • 只是解释如何解决他们问的问题,而不是代码中的所有其他问题(我看到了几个)。
    • 怎么样? AFAICT 它只是不能解决现有问题(除了他们所询问的问题)......我所做的只是将声明移到循环之外。
    • 注意:重新声明结构 point 也可能是 C++ 中的问题。
    • @nemequ 您不能声明具有未指定数量的元素和零初始值设定项的数组。
    • 这不是一个新问题。原代码也存在同样的问题。
    【解决方案3】:

    您在循环内部声明 mypoint,它应该在循环之前。除此之外,数组还需要一个大小,如果你有一个未确定的大小,你可能需要一个不同的数据类型,比如向量。最后,我认为你应该只使用point mypoint... 而不是struct point mypoint...。您当前拥有的内容将创建一个结构结构,但是即使这样也不会编译,因为您已经使用名称“point”来定义您的原始结构并且您不能再次使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-02
      • 2022-10-20
      • 2020-05-31
      • 2023-03-09
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多