gpt4 book ai didi

c++ - 如何在此 C++ 示例中正确使用枚举?

转载 作者:太空宇宙 更新时间:2023-11-03 10:29:17 25 4
gpt4 key购买 nike

我有一个类似这样的类,其中声明了一个公共(public)枚举:

// DataStorage.h
class DataStorage
{
enum ActionType { ADD, REMOVE };

public :
data();
void update(ActionType action, Data dataItem);
};

然后我有一个像这样使用它的 Main.cc ...

// Main.cc
#include "DataStorage.h"

int main() {
DataStorage dataStorage;
Data dataItem("abc123");
dataStorage.update(ActionType.ADD, dataItem); <<-- error here
}

我的问题是当我编译时出现这个错误:

Main.cc:29:18: error: ‘ActionType’ was not declared in this scope

我做错了什么?应该在哪里声明枚举以及如何在 main 中使用它?

谢谢!

最佳答案

更新

dataStorage.update(ActionType.ADD, dataItem);  

到:

dataStorage.update(DataStorage::ADD, dataItem);  

注意:您需要将枚举类型 ActionType 公开

class DataStorage
{
public :
enum ActionType { ADD, REMOVE };

data();
void update(ActionType action, Data dataItem);
};

关于c++ - 如何在此 C++ 示例中正确使用枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21667130/

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