在下面的问题上挣扎太久了,需要一些帮助!
尝试从不同的标题访问结构的成员,但内存对我做错的事情有点模糊!我似乎记得我需要用typedef做一些奇怪的事情吗?
parseinput.h
#ifndef PARSEINPUT_H
#define PARSEINPUT_H
extern struct pitching_stat_struct pitching_home_player;
#endif
parseinput.cpp
struct pitching_stat_struct
{
char playerID[16] = { '\0' };
char teamID[16] = { '\0' };
unsigned short W = 0;
unsigned short L = 0;
unsigned short G = 0;
} pitching_home_player;
someother.cpp
#include "parseinput.h"
void getStructElement()
{
unsigned short playersW = pitching_home_player.W;
// Results in below errors:
// error C2027: use of undefined type 'pitching_stat_struct
// error C2228: left of '.W' must have class/struct/union
}
感谢帮助。
答案 0 :(得分:2)
翻译单元不足以拥有您想要使用的类型的对象。它还需要知道它是如何定义的。仅包括parseinput.h并没有给出它的定义。为了做到这一点,你需要
<强> parseinput.h 强>
#ifndef PARSEINPUT_H
#define PARSEINPUT_H
struct pitching_stat_struct
{
char playerID[16] = { '\0' };
char teamID[16] = { '\0' };
unsigned short W = 0;
unsigned short L = 0;
unsigned short G = 0;
}
extern pitching_stat_struct pitching_home_player;
#endif
<强> parseinput.cpp 强>
#include "parseinput.h"
pitching_stat_struct pitching_home_player;
<强> someother.cpp 强>
#include "parseinput.h"
void getStructElement()
{
unsigned short playersW = pitching_home_player.W;
//...
}