c++visual-c++headernamespacesstdafx.h

Namespace declared in Header file is not being recognized in Source File


What am i doing wrong here?

APP.h

#pragma once

namespace App{

    enum class AppStatus{
    Exit,
    Menu,
    Run
    };

    void frameLoop();

    AppStatus state;

}

App.cpp

#include "App.h"
#include "stdafx.h"
#include <Graphic\Graphic.h>


void App::frameLoop()
{
    while (state != AppStatus::Exit) {

        Graphic::renderingSequence();
    }
}

Errors

Error   C2653   'App': is not a class or namespace name App 
Error   C2065   'state': undeclared identifier  App 
Error   C2653   'AppStatus': is not a class or namespace name   App 
Error   C2065   'Exit': undeclared identifier   App     

Note that my namespace Graphic (declared in \Graphic\Graphic.h) is being recognized by the compiler, even though i declared it just the same way.


Solution

  • stdafx.h (Microsoft precompiled header) must be at the top. This applies to any Visual C++ project with the precompiled headers option turned on, and stdafx.h the standard pch. Those are the default settings for a new project.

    Purpose of stdafx.h

    The simplest and least error-prone way to define the function within the namespace App is just to put it there.

    APP.CPP

    #include "stdafx.h" // Nothing goes above this
    #include "App.h"
    #include <Graphic\Graphic.h>
    
    namespace App {
        void frameLoop()
        {
            while (state != AppStatus::Exit) {
                Graphic::renderingSequence();
            }
        }
    }