Willy Goat
Willy Goat

Reputation: 1203

Private Typedefs in Headers?

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

Upvotes: 1

Views: 842

Answers (2)

melak47
melak47

Reputation: 4850

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>());

Upvotes: 2

LogicStuff
LogicStuff

Reputation: 19617

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

Upvotes: 1

Related Questions