2010-08-31 63 views
1

我與這個主cpp文件:的extern問題結構C++的陣列

#include "stdafx.h" 
#include "Form1.h" 
#include <iostream> 
... 
#include <stdio.h> 

const int MAX_LEN = 1000; 

struct DataLine { 
    char StartCode; 
    int ByteCount; 
    int Address; 
    int RecType; 
    int DBytes[16]; 
    int Checksum; 
}; 
DataLine AllData[MAX_LEN]; 

然後我有以下內容的form.h:

extern const int MAX_LEN; 
extern struct DataLine AllData[MAX_LEN]; 
//later on in header file 
AllData[index].Startcode = sc; 
AllData[index].ByteCount = i_Byte_Count; 
... 

這不會給編譯大量的錯誤,但第一個是:'DataLine *' : unknown size。我應該改變某些東西給typedef嗎?我不確定爲什麼它不喜歡這個。

+1

我不確定'extern const int MAX_LEN = 4033;'很有意義。如果它是一個'extern'變量,你不應該給它一個價值! – 2010-08-31 20:25:13

+0

啊,是的。我修正了這一點。仍然有相同的錯誤。 – 2010-08-31 20:28:14

+0

主要問題已由其他人給出的答案解決。但是,我想我應該指出你的頭文件在其他方面有點奇怪。在一個頭文件中執行賦值(例如'AllData [index] .StartCode = sc;')沒有任何意義,除非碰巧直接將'#include'包含在函數的主體中。 – 2010-08-31 20:33:05

回答

2

在頭文件中不能定義

extern struct DataLine AllData[MAX_LEN]; 

因爲struct DataLine是在頭文件完全未知。沒有typedef會幫助你在這裏。在定義AllData之前,struct DataLine的定義必須出現在頭文件中。把它移到那裏。

1

由於結構的DataLine的declaraction具有的AllData

基本上定義之前form.h,你說你的其他文件,他們可以說:

DataLine* pre = &AllData[5]; 

現在,如何編譯器是否可以知道該項目的起始位置,除非它確切知道每個DataLine有多大?