小编The*_*des的帖子

如何在ANSI C中的结构中使用枚举?

以下代码必须在main-function中使用,但我不知道它是如何使用的.

struct SomeItem
{
    enum {MOVIE, MUSIC} itemType;
    union {
        struct Movie* movie;
        struct Music* music;
    };
};
Run Code Online (Sandbox Code Playgroud)

这个结构用在带有previous/item/next指针的动态链表中,但我不知道如何设置枚举.或者如何初始化它.

我需要知道它在main函数中的样子.

biglist.someitem = ???;
/* declaration I use */
struct Library* biglist;
Run Code Online (Sandbox Code Playgroud)

更多代码,以了解我想要做什么.

struct Library{
struct SomeItem* someitem;
struct SomeItem* previousItem;
struct SomeItem* nextItem;
};
Run Code Online (Sandbox Code Playgroud)

编译错误:C2037:'someitem'左边指定未定义的struct/union'library'C2065:MOVIE:未声明的标识符

我仍然是ANSI C的新秀,所以不要拍我好;)

c enums struct initialization declaration

16
推荐指数
1
解决办法
4万
查看次数

c ++使用结构中的指针保存和加载游戏

我知道我可以用:

MyGame  game; // the game object
//

ofstream out("mygame.bin", ios::binary);
out.write((char *)&game, sizeof(MyGame));
Run Code Online (Sandbox Code Playgroud)

保存并加载游戏,但如果我在MyGame结构中有指针怎么办?指针只会被保存但不会指向它指向的数据吗?

并且:如何解决这个问题?

c++ struct pointers file save

5
推荐指数
1
解决办法
2473
查看次数

标签 统计

struct ×2

c ×1

c++ ×1

declaration ×1

enums ×1

file ×1

initialization ×1

pointers ×1

save ×1