0

I'm trying to move my previous "typed" declaration of std::function vector to work with any type.

This is the declaration code example that work from where I started:

enum STATE {OFF, ON};
class withState {
    private:
        STATE state;
        using stateChangeHandlersFunc = std::function<void(STATE oldState, STATE newState)>;
        std::vector<stateChangeHandlersFunc> stateChangeHandlers;
        void _onStateChange(STATE oldState, STATE newState);
        void setState(STATE /*state*/);
        STATE getState();
        void addStateChangeHandler(stateChangeHandlersFunc /*function*/);
    public:
        withState();
        virtual ~withState();
};

This is the H file of what I'm trying to do: I define a template with a typename StateType which can take different ENUM types, and try to apply this templated type like this.

template<typename StateType>
class withState {
    private:
    protected:
        StateType state;
            using stateChangeHandlersFunc = std::function<void(StateType oldState, StateType newState)>; 
 // <error-type>
        std::vector<stateChangeHandlersFunc> stateChangeHandlers;
        void _onStateChange(StateType oldState, StateType newState);
        void setState(StateType /*state*/);
        StateType getState();
        void addStateChangeHandler(stateChangeHandlersFunc /*function*/);
    public:
        withState();
        virtual ~withState();
};

And the cpp file of my definitions:

#include <thread>

#include "withState.h"


template<typename StateType>
withState<StateType>::withState() {}

template<typename StateType>
void withState<StateType>::_onStateChange(StateType oldState, StateType newState) {
    for(auto&& handler : this->stateChangeHandlers) {
        std::thread(handler, oldState, newState).detach();
    }
}

template<typename StateType>
void withState<StateType>::setState(StateType state) {
    if (this->state != state) {
       this->_onStateChange<StateType>(this->state, state);
       this->state = state;
    }
}

template<typename StateType>
StateType withState<StateType>::getState() {
    return this->state;
}

template<typename StateType>
void withState<StateType>::addStateChangeHandler(stateChangeHandlersFunc function) {
    this->stateChangeHandlers.push_back(function);
}

template<typename StateType>
withState<StateType>::~withState() {}

template class withState<STATE>;
template class withState<VMC_STATE>;
template class withState<VERRIERE_STATE>;

how can I achieve this?

JB_DELR
  • 737
  • 1
  • 4
  • 7
  • Putting the member function definitions in a `.cpp` file is usually (almost always) the wrong thing to do. Put them in the header file. You also have some syntactical errors. `template withState::~withState {}` should be `template withState::~withState() {}` etc. [example](https://godbolt.org/z/8bM6Knj64) – Ted Lyngmo Feb 20 '23 at 17:10
  • 2
    (note that the error I mentioned was not the only one) - Have you read [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file)? – Ted Lyngmo Feb 20 '23 at 17:19
  • Yes, saw that, also I declare each type at the end of the cpp file to make the compiler making each "typed" class. – JB_DELR Feb 20 '23 at 17:21
  • 3
    Which are the errors? – Jarod42 Feb 20 '23 at 17:21
  • using withState::stateChangeHandlersFunc = on this line: using stateChangeHandlersFunc = std::function – JB_DELR Feb 20 '23 at 17:27
  • Please put the full error in the question. The only error I get is on the line `this->_onStateChange(this->state, state);` – Ted Lyngmo Feb 20 '23 at 17:30
  • 3
    "" is not something a compiler would say. And it works fine [here](https://coliru.stacked-crooked.com/a/3f9d8d0b7996a177) after fixing `this->_onStateChange(...)`. Please read about the [mcve]. – molbdnilo Feb 20 '23 at 17:31
  • you need to include the complete verbatim code and error message in the quesiton. If "VS CODE says: using withState::stateChangeHandlersFunc = " is all you get as information then you need to get better tools. I don't know VS Code, I suppose its just an IDE, and the text you added is not actually the compiler error, but just some (useless) summary of it provided by your IDE. If you want to fix errors in your code you must find out where you can read the compilers output, rather than only a summary of it – 463035818_is_not_an_ai Feb 20 '23 at 17:33
  • ho! Thanks for the live, I forgot to include – JB_DELR Feb 20 '23 at 17:34
  • VSCode is not a C++ compiler. Only a C++ compiler fully understands C++ code. If your text editor, VSCode, does not understand your C++ code it does not mean that it's wrong. It only means that VSCode does not understand your C++ code. – Sam Varshavchik Feb 20 '23 at 17:34
  • The `this->_onStateChange` is clearly an incorrect expression. `_onStateChange` is not a template. Everything crumbles after that and diagnostics depend on compiler – Swift - Friday Pie Feb 20 '23 at 17:35
  • Yes I Know. I use Intellisense on VS Code, so it give me some tips before I compile. My program contains big libraries wich take time to compile. So Intellisense Is not so bad to have a preview of what the compiler will says. – JB_DELR Feb 20 '23 at 17:38
  • It compiles now. Thanks to all of you. – JB_DELR Feb 20 '23 at 17:51
  • 1
    @JB_DELR You're welcome! Next time, go for a [mre] and the missing headers will probably be spotted directly. Now I just added those missing because my compiler was pretty explicit about it. :-) – Ted Lyngmo Feb 20 '23 at 17:54

0 Answers0