Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C++;?_C++_Arrays_Struct - Fatal编程技术网

C++ 如何在C++;?

C++ 如何在C++;?,c++,arrays,struct,C++,Arrays,Struct,错误是说: “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++){

错误是说: “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;
}
struct point lineangle(int-x1、int-y1、int-x2、int-y2、int-n){
双角度=360/n,s,c;
int旋转x,旋转y;
DDA(x1,y1,x2,y2);

for(int i=0;i这里的问题是您在for循环的内部声明了
mypoint
,因此它超出了返回值的范围。请尝试将声明移动到for循环之前

struct point mypoint[]={};

for(int i=0;i这里的问题是您在for循环的内部声明了
mypoint
,因此它超出了返回值的范围。请尝试将声明移动到for循环之前

struct point mypoint[]={};

对于(int i=0;i您在循环内部声明mypoint,它应该在循环之前。除此之外,数组还需要一个大小,如果您有一个未确定的大小,您可能需要一个不同的数据类型,如向量。最后,我认为您应该只使用
point mypoint…
而不是
struct point mypoint…
。您认为呢您当前拥有的将创建一个struct of structs,但是即使这样也不会编译,因为您已经使用了名称“point”若要定义原始结构,您不能再次使用它。

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

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;
}
typedef结构点
{
int x;
int-y;
};
结构点*线性角(整数x1、整数y1、整数x2、整数y2、整数n){
双角度=360/n,s,c;
int旋转x,旋转y;
DDA(x1,y1,x2,y2);
int i;
结构点*mypoint=malloc(n*sizeof(结构点));
对于(i=0;i1),在声明中添加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;
}
typedef结构点
{
int x;
int-y;
};
结构点*线性角(整数x1、整数y1、整数x2、整数y2、整数n){
双角度=360/n,s,c;
int旋转x,旋转y;
DDA(x1,y1,x2,y2);
int i;
结构点*mypoint=malloc(n*sizeof(结构点));


对于(i=0;选择C标签或C++标签。这回答了你的问题吗?@ VladfromMoscow为什么你要把这个标记给C?标题是C++,@ ChIPST,因为他总是使用带有结构类型标识符的关键字结构。它看起来是C代码。@ VladfromMoscow,虽然看起来像C,但可能是因为前C程序员是P。不幸的是,标题是我们现在必须要做的。选择C++标签或者C++标签。这回答了你的问题吗?@ VladfromMoscow为什么你要把这个标记给C?标题是C++。@ ChIPST,因为他总是使用带有结构类型标识符的关键字结构。它看起来是C代码。@ VladfromMoscow WHI这看起来像C,可能是因为前C程序员在C++编程。不幸的是,标题是我们现在必须要做的。它只是解释如何解决他们问的问题,而不是代码中的所有其他问题(我看到几个)。怎么?它根本解决不了现存的问题。(除了他们询问的那个)我所做的就是把声明移到循环之外。注意:在C++中重新声明结构“代码>点/代码>也是一个问题。@ NEMQUE,您可能不声明一个数组,它包含了未指定的元素和零初始化器。这不是一个新问题。在原始代码中存在同样的问题。em他们询问的不是代码中的所有其他问题(我看到了一些)。怎么会这样?AFAICT只是不能解决现有的问题(除了他们询问的问题)我所做的就是把声明移到循环外。注意:在C++中重新声明结构“代码>点/代码>也是一个问题。@ NEMQUE,您可能不声明一个数组,它包含了未指定的元素数量和零初始化器。这不是一个新问题。原始代码中存在同样的问题。