简体   繁体   中英

extern problems with array of structures c++

I have the main .cpp file with this:

#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];  

Then I have a form.h with the following:

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;  
...

This will not compile giving a host of errors but the first is: 'DataLine *' : unknown size . Should I change certain things to typedef? I'm not really sure why its not liking this.

You can't define

extern struct DataLine AllData[MAX_LEN];

in the header file because struct DataLine is completely unknown in the header file. No typedef will help you here. The definition of struct DataLine must be present in the header file before you define AllData . Move it there.

Because the declaraction of struct DataLine has to be form.h before the definition of AllData

Basically, you are saying to your other files, that they could say:

 DataLine* pre = &AllData[5];

Now, how could the compiler know where far from the start of AllData that item is, unless it knows exactly how big each DataLine is?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM