gpt4 book ai didi

c++ - 访问未声明的结构?

转载 作者:行者123 更新时间:2023-12-02 02:22:34 25 4
gpt4 key购买 nike

我有办法访问尚未声明的结构吗?

//Need to some how declare 'monitor' up here, with out moving 'monitor' above 'device'
//because both structs need to be able to access each others members

struct{
int ID = 10;
int Get__Monitor__ID(){
return monitor.ID; //obvioulsly 'monitor' is not declared yet, therefore throws error and is not accessible
}
} device;

struct{
int ID = 6;
int Get__Device__ID(){
return device.ID; //because 'device' is declared above this struct, the ID member is accessible
}
} monitor;

最佳答案

在这种特殊情况下,您可以在结构体中定义函数原型(prototype),并且定义可以稍后进行。

struct device_t {
int ID = 10;
int Get__Monitor__ID();
} device;

struct monitor_t {
int ID = 6;
int Get__Device__ID();
} monitor;

int device_t::Get__Monitor__ID() {
return monitor.ID;
}

int monitor_t::Get__Device__ID() {
return device.ID;
}

关于c++ - 访问未声明的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66190901/

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