结构点未由gcc识别 [英] struct point not identified by gcc

查看:59
本文介绍了结构点未由gcc识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个名为point的3D坐标写了一个简单的程序。我是

加两点,占用他们的距离等.gcc编译器不是

识别结构。这是代码。我收到一堆错误。

主要是:在空声明中使用无用的存储类说明符,

''指''未声明(在此函数中首次使用),''p1''未声明

(首次使用此功能)等任何帮助将不胜感激。谢谢


#include< stdio.h>

#include< math.h>


/ *创建一个3d点结构* /

typedef结构点{

int x,y,z;

};


/ *函数声明* /

float dist(点*,点*);

点add(点*,点*);

无效显示(点*);


int main(无效)

{

点p1 ,p2,p3;

p1.x = 2; p1.y = 4; p1.z = 5;

p2.x = 5; p1.y = 2 ; p1.z = 6;

printf(" \\\
dist =%f",dist(& p1,& p2));

add( & p1,& p2);

显示(& p3);

返回;

}

float dist(点* a,点* b)

{

浮动长度= 0.0;

长度= sqrt( (a-> xb-> x)*(a-> xb-> x)+(a-> yb-> y)*(a-> yb-> y)+

(a-> zb-> z)*(a-> zb-> z));

返回长度;

}


点加(点* a,点* b)

{

点温度;

temp.x = a-> x + b-> x;

temp。 y = a-> y + b-> y;

temp.z = a-> z + b-> z;

返回temp; < br $>
}


无效显示(点* a)

{

printf(" \ npoint =%d,%d,%d \ n",a-> x,a-> y,a-> z);

}

I wrote a simply program with a 3 D coordinate called point. I am
adding two points, taking their distance etc. gcc compiler is not
identifying the struct. Here is the code. I get bunch of errors. The
main ones are : useless storage class specifier in empty declaration,
''point'' undeclared (first use in this function), ''p1'' undeclared
(first use in this function) etc. Any help will be appreciated. Thanks

#include <stdio.h>
#include <math.h>

/* create a 3d point struct*/
typedef struct point{
int x,y,z;
};

/* Function declaration*/
float dist(point *, point *);
point add(point *, point *);
void display(point *);

int main(void)
{
point p1,p2,p3;
p1.x=2;p1.y=4;p1.z=5;
p2.x=5;p1.y=2;p1.z=6;
printf("\n dist = %f", dist(&p1,&p2));
add(&p1, &p2);
display(&p3);
return;
}

float dist(point *a, point *b)
{
float length=0.0;
length = sqrt( (a->x-b->x)*(a->x-b->x)+ (a->y-b->y)*(a->y-b->y)+
(a->z-b->z)*(a->z-b->z));
return length;
}

point add(point *a, point *b)
{
point temp;
temp.x=a->x + b->x;
temp.y=a->y + b->y;
temp.z=a->z + b->z;
return temp;
}

void display(point *a)
{
printf("\npoint= %d,%d,%d\n",a->x,a->y,a->z);
}

推荐答案

2月22日上午9:27,DanielJohnson < diffuse ... @ gmail.comwrote:
On Feb 22, 9:27 am, "DanielJohnson" <diffuse...@gmail.comwrote:

我写了一个名为point的3D坐标的简单程序。我是

加两点,占用他们的距离等.gcc编译器不是

识别结构。这是代码。我收到一堆错误。

主要是:在空声明中使用无用的存储类说明符,

''指''未声明(在此函数中首次使用),''p1''未声明

(首次使用此功能)等任何帮助将不胜感激。谢谢


#include< stdio.h>

#include< math.h>


/ *创建一个3d点结构* /

typedef结构点{

int x,y,z;


};


/ *函数声明* /

float dist(点*,点*);

点添加(点*,点*);

void display(point *);
I wrote a simply program with a 3 D coordinate called point. I am
adding two points, taking their distance etc. gcc compiler is not
identifying the struct. Here is the code. I get bunch of errors. The
main ones are : useless storage class specifier in empty declaration,
''point'' undeclared (first use in this function), ''p1'' undeclared
(first use in this function) etc. Any help will be appreciated. Thanks

#include <stdio.h>
#include <math.h>

/* create a 3d point struct*/
typedef struct point{
int x,y,z;

};

/* Function declaration*/
float dist(point *, point *);
point add(point *, point *);
void display(point *);



尝试:

typedef struct tag_point {

int x,y,z;

} point;

Try:
typedef struct tag_point {
int x,y,z;
} point;


>来自C FAQ:
>From the C FAQ:



第2节。结构,工会和枚举


2.1:这两个声明之间有什么区别?


struct x1 {...};

typedef struct {...} x2;


答:第一个表格声明了一个结构标签;第二个声明了一个

" typedef"。主要区别在于您随后将第一种类型称为struct x1并将第一种类型称为struct x1。第二个简单地称为x2。

也就是说,第二个声明是一个稍微抽象的b / b $ b类型 - 它的用户不一定知道它是一个结构,

并且在声明它的实例时不使用关键字struct。


2.2:为什么不
< br $>
struct x {...};

x thestruct;


工作?


答:C不是C ++。

结构标记不会自动生成Typedef名称。另见上面的问题2.1。


[snip]

Section 2. Structures, Unions, and Enumerations

2.1: What''s the difference between these two declarations?

struct x1 { ... };
typedef struct { ... } x2;

A: The first form declares a "structure tag"; the second declares a
"typedef". The main difference is that you subsequently refer
to the first type as "struct x1" and the second simply as "x2".
That is, the second declaration is of a slightly more abstract
type -- its users don''t necessarily know that it is a structure,
and the keyword struct is not used when declaring instances of it.

2.2: Why doesn''t

struct x { ... };
x thestruct;

work?

A: C is not C++. Typedef names are not automatically generated for
structure tags. See also question 2.1 above.

[snip]


DanielJohnson写道:
DanielJohnson wrote:

我写了一个名为point的3D坐标的简单程序。我是

加两点,占用他们的距离等.gcc编译器不是

识别结构。这是代码。我收到一堆错误。

主要是:在空声明中使用无用的存储类说明符,

''指''未声明(在此函数中首次使用),''p1''未声明

(首次使用此功能)等任何帮助将不胜感激。谢谢


#include< stdio.h>

#include< math.h>


/ *创建一个3d点结构* /

typedef结构点{

int x,y,z;

};
I wrote a simply program with a 3 D coordinate called point. I am
adding two points, taking their distance etc. gcc compiler is not
identifying the struct. Here is the code. I get bunch of errors. The
main ones are : useless storage class specifier in empty declaration,
''point'' undeclared (first use in this function), ''p1'' undeclared
(first use in this function) etc. Any help will be appreciated. Thanks

#include <stdio.h>
#include <math.h>

/* create a 3d point struct*/
typedef struct point{
int x,y,z;
};



更改为:


typedef struct {

int x;

int y;

int z;

} point;

Change this to:

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


/ *函数声明* /

float dist(点*,点*);

点加(点*,点*);

无效显示(点* );


int main(无效)

{

点p1,p2,p3;

p1.x = 2; p1.y = 4; p1.z = 5;

p2.x = 5; p1.y = 2; p1.z = 6;
/* Function declaration*/
float dist(point *, point *);
point add(point *, point *);
void display(point *);

int main(void)
{
point p1,p2,p3;
p1.x=2;p1.y=4;p1.z=5;
p2.x=5;p1.y=2;p1.z=6;



为什么不使用更多的空格来帮助清晰?您似乎也在

覆盖p1.y和p1.z中的值。

Why not use more whitespace to aid in clarity? You also seem to be
overwriting the values in p1.y and p1.z.


printf(" \\\
dist = %f",dist(& p1,& p2));
printf("\n dist = %f", dist(&p1,&p2));



在%f之后添加换行符。另外,根据上面给出的初始化结果,p2.y,p2.z,p3.x,p3.y,p3.z都是未初始化的。如果你使用未初始化的物品操作
,你很可能会回来无用

结果。

Add a newline after %f as well. Also as per the initialisations given
above, p2.y, p2.z, p3.x, p3.y, p3.z are all uninitialised. If you
operate on uninitialised objects you''ll most likely get back useless
results.


add (& p1,& p2);
add(&p1, &p2);



函数add返回一个本地对象(如下所示)。你不是在这里给任何东西分配返回值,从而失去它。

The function add returns a local object, (as given below). You''re not
assigning the return value to anything here, thus loosing it.


display(& p3);

返回;
display(&p3);
return;



返回一个显式值;不要返回随机值。

Return an explicit value; don''t return random values.


}


float dist(point * a,point * b)

{

浮动长度= 0.0;

长度= sqrt((a-> xb-> x)*(a-> xb - > x)+(a-> yb-> y)*(a-> yb-> y)+

(a-> zb-> z)* (A-> ZB-&将Z));
}

float dist(point *a, point *b)
{
float length=0.0;
length = sqrt( (a->x-b->x)*(a->x-b->x)+ (a->y-b->y)*(a->y-b->y)+
(a->z-b->z)*(a->z-b->z));



您确实意识到b-> y和b-> z未初始化不是吗?你可能还想把凌乱的函数调用分成更可读的步骤。

You do realise that b->y and b->z are uninitialised don''t you? You
might also want to split up the messy function call into more readable
steps.


返回长度;

}


点加(点* a,点* b)

{

点温度;

temp.x = a-> x + b-> x;

temp.y = a-> y + b-> y ;

temp.z = a-> z + b-> z;

返回临时数;

}


无效显示(点* a)

{

printf(" \ npoint =%d,%d,%d \ n" ;,a-> x,a-> y,a-> z);

}
return length;
}

point add(point *a, point *b)
{
point temp;
temp.x=a->x + b->x;
temp.y=a->y + b->y;
temp.z=a->z + b->z;
return temp;
}

void display(point *a)
{
printf("\npoint= %d,%d,%d\n",a->x,a->y,a->z);
}



纠正这些错误,尝试再次编译。

Correct these errors and try to compile again.


2月22日,下午12:43,santosh < santosh .... @ gmail.comwrote:
On Feb 22, 12:43 pm, "santosh" <santosh....@gmail.comwrote:

DanielJohnson写道:
DanielJohnson wrote:

我写了一个只需使用称为点的3D坐标编程。我是

加两点,占用他们的距离等.gcc编译器不是

识别结构。这是代码。我收到一堆错误。

主要是:在空声明中使用无用的存储类说明符,

''指''未声明(在此函数中首次使用),''p1''未声明

(首次使用此功能)等任何帮助将不胜感激。谢谢
I wrote a simply program with a 3 D coordinate called point. I am
adding two points, taking their distance etc. gcc compiler is not
identifying the struct. Here is the code. I get bunch of errors. The
main ones are : useless storage class specifier in empty declaration,
''point'' undeclared (first use in this function), ''p1'' undeclared
(first use in this function) etc. Any help will be appreciated. Thanks


#include< stdio.h>

#include< math.h>
#include <stdio.h>
#include <math.h>


/ *创建一个3d点结构* /

typedef struct point {

int x,y,z;

};
/* create a 3d point struct*/
typedef struct point{
int x,y,z;
};



更改为:


typedef struct {

int x;

int y;

int z;

} point;


Change this to:

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


/ *函数声明* /

float dist(点*,点*);

点加(点*,点*);

无效显示(点* );
/* Function declaration*/
float dist(point *, point *);
point add(point *, point *);
void display(point *);


int main(void)

{

point p1,p2,p3;

p1.x = 2; p1.y = 4; p1.z = 5;

p2.x = 5; p1.y = 2; p1.z = 6 ;
int main(void)
{
point p1,p2,p3;
p1.x=2;p1.y=4;p1.z=5;
p2.x=5;p1.y=2;p1.z=6;



为什么不使用更多的空格来帮助清晰?您似乎也在

覆盖p1.y和p1.z中的值。


Why not use more whitespace to aid in clarity? You also seem to be
overwriting the values in p1.y and p1.z.


printf(" \\\
dist = %f",dist(& p1,& p2));
printf("\n dist = %f", dist(&p1,&p2));



在%f之后添加换行符。另外,根据上面给出的初始化结果,p2.y,p2.z,p3.x,p3.y,p3.z都是未初始化的。如果你使用未初始化的物品操作
,你很可能会回来无用

结果。


Add a newline after %f as well. Also as per the initialisations given
above, p2.y, p2.z, p3.x, p3.y, p3.z are all uninitialised. If you
operate on uninitialised objects you''ll most likely get back useless
results.


add (& p1,& p2);
add(&p1, &p2);



函数add返回一个本地对象(如下所示)。你不是在这里给任何东西分配返回值,从而失去它。


The function add returns a local object, (as given below). You''re not
assigning the return value to anything here, thus loosing it.


display(& p3);

返回;
display(&p3);
return;



返回一个显式值;不要返回随机值。


Return an explicit value; don''t return random values.


}
}


float dist(point * a,point * b)

{

浮动长度= 0.0;

长度= sqrt((a-> xb-> x )*(a-> xb-> x)+(a-> yb-> y)*(a-> yb-> y)+

(a-> ; ZB-&将Z)*(A-> ZB-&将Z));
float dist(point *a, point *b)
{
float length=0.0;
length = sqrt( (a->x-b->x)*(a->x-b->x)+ (a->y-b->y)*(a->y-b->y)+
(a->z-b->z)*(a->z-b->z));



您确实意识到b-> y和b-> z未初始化不是吗?你可能还想把凌乱的函数调用分成更可读的步骤。


You do realise that b->y and b->z are uninitialised don''t you? You
might also want to split up the messy function call into more readable
steps.


返回长度;

}
return length;
}


point add(point * a,point * b)

{

点温度;

temp.x = a-> x + b-> x;

temp.y = a-> ; y + b-> y;

temp.z = a-> z + b-> z;

返回温度;

}
point add(point *a, point *b)
{
point temp;
temp.x=a->x + b->x;
temp.y=a->y + b->y;
temp.z=a->z + b->z;
return temp;
}


无效显示(点* a)

{

printf(" ; \ npoint =%d,%d,%d \ n",a-> x,a-> y,a-> z);

}
void display(point *a)
{
printf("\npoint= %d,%d,%d\n",a->x,a->y,a->z);
}



纠正这些错误并尝试再次编译。


Correct these errors and try to compile again.



我做了所有更改,现在我收到了这个错误


undefined reference tosqrt'


math.sqrt()是正确的还是只是sqrt()

I did all your changes and now I get this error

undefined reference to `sqrt''

Is math.sqrt() correct or just sqrt()


这篇关于结构点未由gcc识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆