我有以下在兩個源檔案中共享的頭檔案:
某事.h:
#define NBALLS 10
#define NBOTS 2
struct {
float px, py, pz, vx, vy, vz;
}bots[NBOTS];
struct {
float px, py, pz, vx, vy, vz;
}balls[NBALLS];
我在兩個源檔案中使用相同的方式:ac:
#include "sth.h"
void makeBalls(void) {
int i;
srand(time(NULL));
for (i=1;i<NBALLS;i ) {
balls[i].px = MIN randf()*(MAX-MIN);
balls[i].py = MIN randf()*(MAX-MIN);
//pz[i] = 0.9;
balls[i].pz = MIN randf()*(MAX-MIN);
balls[i].vx = -1.5 randf()*3.0;
balls[i].vy = -1.5 randf()*3.0;
balls[i].vz = -1.0 randf()*2.0;
}
}
和公元前:
#include "sth.h"
void makeBots(void) {
int i;
srand(time(NULL));
for (i=1;i<NBOTS;i ) {
bots[i].px = MIN randf()*(MAX-MIN);
bots[i].py = MIN randf()*(MAX-MIN);
//pz[i] = 0.9;
bots[i].pz = MIN randf()*(MAX-MIN);
bots[i].vx = -1.5 randf()*3.0;
bots[i].vy = -1.5 randf()*3.0;
bots[i].vz = -1.0 randf()*2.0;
}
}
在 HP-UX 中,它可以正常構建和運行。在最新的 macOS 中,它抱怨重復的符號。
如果我在 sth.h 中進行以下更改:
static struct {
float px, py, pz, vx, vy, vz;
}bots[NBOTS];
static struct {
float px, py, pz, vx, vy, vz;
}balls[NBALLS];
它構建并運行良好。但是,自然地,我的程式不起作用。
我在這里嘗試遵循建議: Declaring an extern struct template in header file for global use in c files
但隨后 Xcode 抱怨說
bots[i].px = MIN randf()*(MAX-MIN);
不是向量或指標。
我很困惑為什么它可以在 HP-UX 下作業。我正在學習 C(非常初學者)并試圖確保我的代碼保持可移植性,并學習如何處理位元組序問題。
謝謝,一如既往。
編輯:我根據建議所做的更改,回答以下評論:sth.h:
#define NBALLS 10
#define NBOTS 2
struct {
float px, py, pz, vx, vy, vz;
}botsZ[NBOTS];
struct {
float px, py, pz, vx, vy, vz;
}ballsZ[NBALLS];
extern struct botsZ bots;
extern struct ballsZ balls;
Xcode 在 ac 和 bc 上回傳:“下標值不是陣列、指標或向量”在 for 回圈內函式的每一行。
為什么 aCC 可以很好地構建它?我認為 MIPSPro 也可以構建它(我會在旅行回來后進行測驗)
uj5u.com熱心網友回復:
botsZ是一個匿名結構的實體陣列,2定義為 htis:
struct {
float px, py, pz, vx, vy, vz;
};
這是該陣列的定義,在頭檔案中:
#define NBOTS 2
struct {
float px, py, pz, vx, vy, vz;
}botsZ[NBOTS];
下列
extern struct botsZ bots;
bots宣告了一個以 type命名的 extern 變數struct botsZ,但沒有這樣的型別。botsZ是一個實體。
建議:
- 在檔案中定義你需要的
structs和stypedef.h extern宣告實體- 在
.c檔案中定義實體
例子:
某事
#ifndef STH_H
#define STH_H
#define NBALLS 10
#define NBOTS 2
typedef struct {
float px, py, pz, vx, vy, vz;
} botsZ;
typedef struct {
float px, py, pz, vx, vy, vz;
} ballsZ;
extern botsZ bots[NBOTS];
extern ballsZ balls[NBALLS];
#endif
某事
#include "std.h"
botsZ bots[NBOTS] = {0};
ballsZ balls[NBALLS] = {0};
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/425805.html
