I'm setting up a finite state machine structure as a proof of concept to be used in other projects. The states have function pointers that point to actions they are allowed to perform during certain events and transitions. I'm trying to restrict these pointers to be a part of a certain namespace, but the compiler doesn't recognize the namespace. The exact error is " StateAction': is not a class or namespace name. I've whittled out code until just these lines below remain.

//In State.h file
#pragma once
#include "StateAction.h"

class CState {
    void addAction(void (StateAction::*funcPtr)()) { m_func = funcPtr; } //error line

    void (StateAction::*m_func)(); //error line

//In StateAction.h file
#pragma once
#include <iostream>

namespace StateAction {
    void fun1();
    void fun2();

//In StateAction.cpp file
#include "StateAction.h"

void StateAction::fun1() { 
    std::cout << "func 1" << std::endl;
void StateAction::fun2() {
    std::cout << "func 2" << std::endl; 

Any suggestions are appreciated. Most searches on this error are about circular dependencies, so I haven't been able to find much info. Is it even possible to restrict a function pointer to a namespace? I'm almost positive I've done this before with classes.

As far as I know, this cannot be done. From a practical point of view, compilers would be required to track in what namespace a particular function a function pointer point to was defined, and I don't think any compiler really does that because there is no real purpose for it, and also because one function can actually belong to a number of different namespaces (yes, that's what a "using somewhere::something;" statement does). From a coding point of view, there is no real value in a feature that seemingly restricts something, when in reality, it imposes no restriction whatsoever. Nothing prevents users of your class from declaring (or "using") their functions in that namespace. I could easily have this:

namespace my_code {

void my_function();


namespace StateAction {

using my_code::my_function;


// And now, you can do:
some_state.addAction(&StateAction::my_function);  // should this be allowed or not?

In any case, there is no way to forbid users from creating functions in that namespace, and thus, the restriction that function pointers must point to functions in that namespace is a very weak restriction.

An alternative might be to require that instead of a function pointer, you must give a polymorphic object from a class derived from some simple base class for all state-actions. Another alternative might be to make the addAction function a private function and then befriend only specific classes or functions, allowing them to be the only ones capable of providing a function pointer for that state class.

Thanks for the info.

I've implemented it as a polymorphic object that contains an 'action' function, which serves as a suitable restriction on what the state can do. I wanted to avoid the possibility of accidentally passing a nonsensical function to the state when writing code, which could more easily happen with plain function pointers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.