gpt4 book ai didi

c++ - 标题中的私有(private) Typedef?

转载 作者:行者123 更新时间:2023-11-28 00:11:22 25 4
gpt4 key购买 nike

编辑:只是忘了添加“state_manager::”我的错。

我正在尝试创建一个简单的状态系统。为了节省一些输入并使以后更容易更改,我在我的 state_manager.hpp 中放置了一些 typedef。问题是这些 typedef 在我的 state_manager.cpp 中似乎无法识别。我收到诸如 'element' does not name a type 之类的错误,奇怪的是 'states' 未在此范围内声明。我真的很困惑。

状态管理器.hpp:

#pragma once
#include <stack>
#include <memory>

class state;

class state_manager{
typedef std::unique_ptr<state> element;
typedef std::stack<element> container;
protected:
container states;
public:
void push(const element &to_push);
void pop();
void change(const element &change_to);
};

状态管理器.cpp:

#include "state_manager.hpp"
#include "state.hpp"

void push(const element &to_push){
states.push(to_push);
}

void pop(){
states.pop();
}

void change(const element &change_to){
states.pop();
push(change_to);
}

最佳答案

除了作为成员函数的缺失限定外,unique_ptr s 不可复制,因此您当前的 push 实现和 change将不起作用。

你可以这样改变它们:

void state_manager::push(element&& to_push) {
states.push(std::forward<element>(to_push));
}

然后可以像my_state_manager.push(std::make_unique<state>());一样使用

关于c++ - 标题中的私有(private) Typedef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32938668/

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