1

Edit: Just forgot to add "state_manager::" My bad.

I am trying to create a simple state system. To save some typing and to make things easier to change later, I've put some typedefs in my state_manager.hpp. The problem is that these typedefs don't seem to be recognized in my state_manager.cpp. I get errors like 'element' does not name a type and strangely 'states' was not declared in this scope. I am really confused by this.

state_manager.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);
};

state_manager.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);
}
Willy Goat
  • 1,175
  • 2
  • 9
  • 24

2 Answers2

2

In addition of the missing qualification as member functions, unique_ptrs are not copyable, so your current implementation of push and change will not work.

You could change them like this:

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

which could then be used like my_state_manager.push(std::make_unique<state>());

melak47
  • 4,772
  • 2
  • 26
  • 37
  • I've made these changes, but I've got an off-topic problem now. Apparently make_unique is c++14, but -std=c++1y doesn't change anything for me. Using gcc 4.8.2. apt-get tells me gcc is up to date. Any suggestions? – Willy Goat Oct 04 '15 at 22:09
  • You'll need at least gcc 4.9. Check if there's an update for your distro, Debian stable for example only recently updated gcc to 4.9. Alternatively, you could [roll your own](http://stackoverflow.com/a/24609331/996886) if you cannot get a newer compiler. – melak47 Oct 04 '15 at 22:17
  • Thank you. I'll try that; I'm sure that's it. For now, I've accidentally started a 30+ minute all-package update. I'll just let it do its thing, haha. Thanks again. – Willy Goat Oct 04 '15 at 22:27
1
void push(const element &to_push){
    states.push(to_push);
}

- you're not defining the member functions but a non-member ones, therefore you don't have access to the private members of your class. You'd get a linker error after that. Don't forget to add the prefix:

void state_manager::push(const element &to_push){
    states.push(to_push);
}
LogicStuff
  • 19,397
  • 6
  • 54
  • 74