2011-04-11 46 views
0
struct 
{ 
    float lat_radians;   //latitude in radians 
    float lon_radians;   //longtiude in radians 
    float alt_radians;   //altiude in radians 
    double ECEF_X;   // ECEF X in metres 
    double ECEF_Y;    // ECEF Y in metres 
    double ECEF_Z;    // ECEF Z in metres 

} gps_position; 

void main() 
{ 
LatLongAlt_to_ECEF_coordinates(struct gps_position {float lon; float lat; float alt} arg); 
} 

我從c141出錯:'struct'附近出現語法錯誤。我試圖弄清楚,但找不到任何解決方案
請幫助.. 感謝將struct中的變量調用到c中的函數中的問題

問候 凱文


感謝您的有用的信息。但我做了一些更改我的代碼,我仍然有錯誤C3861的問題:'lla_to_ECEF':標識符未找到&錯誤C2365:'lla_to_ECEF':重新定義;以前的定義是'以前未知的標識符'我可以知道我該如何糾正我的錯誤?謝謝kevin

#include <stdio.h> 

struct gps_position 
{ 
float alt; 
float lon; 
float lat; 
double ECEF_x; 
}; 

void main() 
{ 

lla_to_ECEF (gps_position.alt); 

return; 
} 

float lla_to_ECEF (float alt) 
{ 

//some calculate to be done 

} 
+0

kevin,僅供將來參考,你可以通過我的格式代碼縮進4個空格,或者在它周圍加反引號。或者您可以突出顯示該代碼,然後按頂部的{}按鈕 – 2011-04-11 04:28:31

回答

4

你需要在struct之後立即聲明。例子見你最喜歡的C書。

struct gps_position 
{ 
    float lat_radians;   //latitude in radians 
    float lon_radians;   //longtiude in radians 
    float alt_radians;   //altiude in radians 
    double ECEF_X;    // ECEF X in metres 
    double ECEF_Y;    // ECEF Y in metres 
    double ECEF_Z;    // ECEF Z in metres 
} ; 

typedef struct 
{ 
     float lat_radians;   //latitude in radians 
     float lon_radians;   //longtiude in radians 
     float alt_radians;   //altiude in radians 
     double ECEF_X;    // ECEF X in metres 
     double ECEF_Y;    // ECEF Y in metres 
     double ECEF_Z;    // ECEF Z in metres 
} gps_position ; 
0

這將消除您的錯誤:

這是通過結構參數成函數關係的正確途徑

 #include <stdio.h> 
    typedef struct 
    { 
     float lat_radians;   //latitude in radians 
     float lon_radians;   //longtiude in radians 
     float alt_radians;   //altiude in radians 
     double ECEF_X;   // ECEF X in metres 
     double ECEF_Y;    // ECEF Y in metres 
     double ECEF_Z;    // ECEF Z in metres 

     } gps_position; 

    void main() 
    { 
     gps_position gps; 
      LatLongAlt_to_ECEF_coordinates(gps); 
    } 
+0

格式化有點混亂:) – 2011-04-11 04:35:19

1

首先,這不是一個有效的聲明一個struct的方法試試這個:

struct gps_position 
{ 
    float lat_radians;   //latitude in radians 
    float lon_radians;   //longtiude in radians 
    float alt_radians;   //altiude in radians 
    double ECEF_X;    // ECEF X in metres 
    double ECEF_Y;    // ECEF Y in metres 
    double ECEF_Z;    // ECEF Z in metres 

}; 

第二,如果你想要一個結構傳遞給函數做:

gps_position arg; 

//Do assigning stuff before the function is called. 

LatLongAlt_to_ECEF_coordinates(arg); 
1

你想給一個標籤到您的gps_position結構開始:

struct gps_position 
{ 
    float lat_radians;   //latitude in radians 
    float lon_radians;   //longtiude in radians 
    float alt_radians;   //altiude in radians 
    double ECEF_X;   // ECEF X in metres 
    double ECEF_Y;    // ECEF Y in metres 
    double ECEF_Z;    // ECEF Z in metres 

}; 

你以前有什麼定義了一個名爲gps_position的變量,但沒有給出結構本身的名稱。

void main() 

不在於它是在手的問題特別重要,但通常main應該返回int

{ 
LatLongAlt_to_ECEF_coordinates(struct gps_position {float lon; float lat; float alt} arg); 
} 

如果要調用一個函數與gps_position,您通常會做這樣的事情:

struct gps_position pos; 

LatLongAlt_to_ECEF_coordinates(pos); 

只有在典型情況下,你想傳遞的指針結構:

LatLongAlt_to_ECEF_coordinates(&pos); 

個人而言,我想我會基於弧度座標從基於ECEF座標分開:

struct radian_coords { 
    float latitude, longitude, altitude; 
} 

struct ECEF_coords { 
    double X, Y, Z; 
} 

然後,當你從一個到另一個轉換,你會通過每個實例:

struct radian_cords r_pos = { 12345.6, 23456.7, 123.4 }; 
struct ECEF_coords e_pos; 

LatLongAlt_to_ECEF_coordinates(&r_pos, &e_pos); 

或者,你可以寫函數返回正確的類型:

e_pos = LatLongAlt_to_ECEF_coordinates(&r_pos); 

在這種情況下,你的函數頭看起來像這樣:

struct e_pos LatLongAlt_to_ECEF_coordinates(struct r_pos const *input) { 
    struct e_pos ret; 
    // compute and assign values to ret.X, ret.Y and ret.Z 
    return ret; 
}