gpt4 book ai didi

c++ - 从不同的 header 访问外部结构成员

转载 作者:行者123 更新时间:2023-11-28 01:34:12 31 4
gpt4 key购买 nike

为以下问题苦苦挣扎了太久,需要一点帮助!

试图从不同的 header 访问结构的成员,但我做错了什么时内存有点模糊!我似乎记得我需要用 typedef 做一些奇怪的事情?

解析输入.h

#ifndef PARSEINPUT_H
#define PARSEINPUT_H

extern struct pitching_stat_struct pitching_home_player;

#endif

解析输入.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
}

感谢您的帮助。

最佳答案

翻译单元拥有您要使用的类型的对象是不够的。它还需要知道它是如何定义的。仅包括 parseinput.h 并没有给它定义。为此,您需要拥有

解析输入.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

解析输入.cpp

#include "parseinput.h"

pitching_stat_struct pitching_home_player;

someother.cpp

#include "parseinput.h"

void getStructElement()
{
unsigned short playersW = pitching_home_player.W;
//...
}

关于c++ - 从不同的 header 访问外部结构成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50070615/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com