0

Okay, I thought I had implementation files for template classes figured out, but apparently not... I have the following files in a VS 2013 C++ solution:

Main.cpp

#include "StateManager.h"
#include "State.h"

enum class Derp {
    Herp,
    Lerp,
    Sherp,
};

int main() {
    Game2D::State<Derp>::Context context(5);
    Game2D::StateManager<Derp> mgr(context);

    return 0;
}

StateManager.h

#pragma once

#include "State.h"

namespace Game2D {

    template<typename Id>
    class StateManager {
    private:
        typename State<Id>::Context _context;

    public:
        explicit StateManager(typename State<Id>::Context context);
    };

#include "StateManager.inl"

}

StateManager.inl

template<typename Id>
StateManager<Id>::StateManager(typename State<Id>::Context context) :
    _context(context)
{ }

State.h

#pragma once

namespace Game2D {

    template<typename Id>
    class StateManager;

    template<typename Id>
    class State {
    public:
        struct Context {
            Context(int);
            int data;
        };

    private:
        StateManager<Id>* _manager;
        Context _context;

    public:
        State(StateManager<Id>&, Context);
        virtual ~State();

    };

#include "State.inl"

}

State.inl

template<typename Id>
State<Id>::Context::Context(int data) {
    this->data = data;
}

template<typename Id>
State<Id>::State(StateManager<Id>& manager, Context context) :
    _manager(&manager),
    _context(context)
{ }
template<typename Id>
State<Id>::~State() { }

Building this Project yields the following errors:

Error 10 error C1903: unable to recover from previous error(s); stopping compilation state.inl 9 1

Error 9 error C2065: 'context' : undeclared identifier state.inl 8 1

Error 7 error C2065: 'manager' : undeclared identifier state.inl 7 1

Error 8 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int state.inl 7 1

Error 6 error C2039: 'State' : is not a member of '`global namespace'' state.inl 6 1

Error 1 error C2143: syntax error : missing ';' before '<' state.inl 2 1

Error 2 error C2988: unrecognizable template declaration/definition state.inl 2 1

Error 3 error C2059: syntax error : '<' state.inl 2 1

Error 4 error C3083: 'Context': the symbol to the left of a '::' must be a type state.inl 2 1

Error 5 error C2039: 'Context' : is not a member of '`global namespace'' state.inl 2 1

Any help on how to fix these errors would be much appreciated!

Community
  • 1
  • 1
Rabadash8820
  • 2,328
  • 3
  • 27
  • 49
  • Try to present an [MCVE](http://stackoverflow.com/help/mcve). If each function generates these errors, leave just one function. Remove unnecessary member declarations and include directives. – n. m. could be an AI Aug 11 '15 at 19:03
  • It fails the M, the C, and the V parts. M: it has many function definitions, one would be enough. C: there's no State.h V: I can't take your example and compile it and get all the same errors you are getting. If this is indeed your source code verbatim, notice that it has many closing braces commented out. – n. m. could be an AI Aug 11 '15 at 19:33
  • Okay, I updated my original post – Rabadash8820 Aug 11 '15 at 20:58
  • Can't reproduce the errors [here](http://ideone.com/tIPtyD). Make sure the files you post are the ones you compile. – n. m. could be an AI Aug 11 '15 at 20:59
  • I've never used Ideone, but I would expect this code to work when its all in a single file. Its the splitting between .h and .inl files that seems to be the problem – Rabadash8820 Aug 11 '15 at 21:04
  • 1
    So, which lines trigger these errors? Which one is the first error? Why does the first error message you quoted mention "previous errors"? What errors are "previous" in this case? – AnT stands with Russia Aug 11 '15 at 21:21
  • AnT, I updated my original post to show more raw error output. As you can see, all errors are being thrown in State.inl, and the error about "previous errors" is the last one – Rabadash8820 Aug 11 '15 at 21:40
  • Your project should reference a single source file and several include files. .inl files should not be among the source files. Is this the case? – n. m. could be an AI Aug 11 '15 at 21:53
  • That is the case. And the Properties Window shows that both inl files have a File Type of "Document" – Rabadash8820 Aug 11 '15 at 22:00
  • My bad, I deleted some comments when I pasted into the post. I updated with the correct line numbers – Rabadash8820 Aug 11 '15 at 22:08
  • @Rabadash8820: Er... Do you by any chance attempt to compile your `.inl` files as standalone files? Do you see these files in the solution tree? Does the output window show these files being compiled by themselves? – AnT stands with Russia Aug 11 '15 at 22:25
  • AnT, that comment was made before I updated my post. The five files above _are_ the ones in the separate solution, and building them is what generates the provided errors. – Rabadash8820 Aug 11 '15 at 22:28
  • @Rabadash8820: So, again, how is that solution organized? Did you add the `.inl` files to the project? When you build the project, do you see these `.inl` files being compiled independently? – AnT stands with Russia Aug 11 '15 at 22:29
  • I created the solution as an Empty Project, so it only has the .h, the .inl, and the .cpp files exactly as shown above. The .h and .inl files are under the Header Files filter and Main.cpp is under the Source Files filter. I'm not quite sure what you mean by ".inl files being compiled independently" but the first two lines of the Build Output say: 1>------ Build started: Project: Derp, Configuration: Debug Win32 ------ 1> State.inl – Rabadash8820 Aug 11 '15 at 23:15

1 Answers1

0

A wild guess would be that you added your .inl files to you your project as standalone translation units and the compiler attempted to compile them as standalone translation units.

These files make no sense as standalone translation units and they will not compile as such. These are include files (aka header files). They are supposed to be seen as header files by the project. They are not supposed to be compiled directly.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • So how do I specify to the compiler that they are not translation units? – Rabadash8820 Aug 11 '15 at 23:39
  • @Rabadash8820: It depends on your compiler. In Visual Studio, for example, you *don't* add them under `Source Files` category. If you want to add them to the project, add them under `Header Files` category. Alternatively, you can simply exclude them from build in project settings. – AnT stands with Russia Aug 11 '15 at 23:41