アプリ版:「スタンプのみでお礼する」機能のリリースについて

・struct.h
struct pos3d{
double x;
double y;
double z;
};
extern double distance(struct pos3d s,struct pos3d t);

・struct.c
#include<stdio.h>
#include<math.h>
#include"struct.h"

struct pos3d{
double x;
double y;
double z;
};
double distance(struct pos3d s,struct pos3d t){
double ans;

ans = sqrt((s.x-t.x)*(s.x-t.x)+(s.y-t.y)*(s.y-t.y)+(s.z-t.z)*(s.z-t.
z));/*2点間の距離の計算*/
return ans;
}

・main.c
#include<stdio.h>
#include<math.h>
#include"struct.h"

int main(){
struct pos3d a;
struct pos3d b;
double c;
printf("1つめのx,y,z座標をそれぞれ入力する。");scanf("%lf,%lf,%lf",&
a.x,&a.y,&a.z);
printf("2つめのx,y,z座標をそれぞれ入力する。");scanf("%lf,%lf,%lf",&
b.x,&b.y,&b.z);

c = distance(a,b);
printf("aの座標(%.2f,%.2f,%.2f),bの座標(%.2f,%.2f,%.2f)\n",a.x,a.y,a.
z,b.x,b.y,b.z);
printf("2点間の距離:%f\n",c);
return 0;
}

cc -Wall -c -o struct.o struct.c

struct.c:5:8: error: redefinition of ‘struct pos3d’
struct pos3d{
^~~~~
In file included from struct.c:3:0:
struct.h:1:16: note: originally defined here
typedef struct pos3d{
^~~~~
上記のようなエラーが出るんですけど、どうしたらいいですか?(できたら、上のプログラムで間違えている場所が有れば指摘が欲しいです。)
また、この後にどのようにコンパイルしていけばよいのか分からないので教えてください。

A 回答 (1件)

エラーメッセージにある通りです。



struct.c:5:8: error: redefinition of ‘struct pos3d’

struct.c の 5行目 の8文字目から
エラー
struct pos3d の再定義

1度定義した構造体と同じ名前の構造体を定義することはでません。
たとえ、内容が同じだとしても。


#include は、指定したファイルがそこにコピペされるようなものだと思ってください。
#include"struct.h"
というのは、そこに struct.h がコピペされた状態、つまり
struct pos3d{
double x;
double y;
double z;
};
extern double distance(struct pos3d s,struct pos3d t);
と書いたのと同じになります。
    • good
    • 0
この回答へのお礼

ありがとありがとうございます!無事解決できました!!

お礼日時:2019/12/11 08:59

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!