gpt4 book ai didi

c++ - 声明一个类/结构

转载 作者:行者123 更新时间:2023-11-30 04:37:23 26 4
gpt4 key购买 nike

例如,我有一个结构,我正在使用类的静态函数来初始化它的成员。我如何告诉编译器该类存在但它是在结构 S 之后定义的?

struct S
{
S()
{
x = C::GetX(); //static functions, GetX() and GetY()
y = C::GetY();
}

int x; int y;
};

class C
{
/.... /
}

编辑:我还将在 class C

中使用 struct S

最佳答案

C 放在第一位。

没有更多上下文,很难更深入地回答。

struct S;

class C
{
/.... /
};

struct S
{
S()
{
x = C::GetX(); //static functions, GetX() and GetY()
y = C::GetY();
}

int x; int y;
};

或者,您可能需要在 class S block 之外定义 S::S

struct S
{
S();

int x; int y;
};

class C
{
/.... /
};

S::S()
{
x = C::GetX(); //static functions, GetX() and GetY()
y = C::GetY();
}

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

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