1

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.

8
  • 1
    #include "stdafx.h" Must always be the first non comment line. All lines above that are ignored by the compiler. Commented Dec 16, 2016 at 21:06
  • This has to be a duplicate. Commented Dec 16, 2016 at 21:06
  • Thanks! will have to learn more about what exactly is special about #include <stdafx.h> Commented Dec 16, 2016 at 21:08
  • @drescherjm I did not find anything that helped me, because i did not expect that #include can have variable functionality Commented Dec 16, 2016 at 21:11
  • 3
    stackoverflow.com/questions/16040689/… Commented Dec 16, 2016 at 21:15

1 Answer 1

2

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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I agree with Jive you should put the function in the namespace rather than saying App::frameLoop(). It might be confusing for people and they might think App is a class.
More importantly, the code in the cpp file will automatically resolve names the same as the .h file does. E.g. AppStatus::Exit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.