Well, looking at your sample project, I think I can see the problem.
You have opened a FileCodeModel
on a .cpp file and you are adding a function to it - but there's no class.
Also, for generating C++, you should make sure you use VCCodeModel
.
What you should do is to create a class first, then add the constructor to the class. Something like this:
// get IDE
EnvDTE.DTE dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.12.0");
// select the second project (pre-created as in your example)
var project = dte.Solution.Projects.Item(2);
// get the VC code model
VCCodeModel pModel = (VCCodeModel)project.CodeModel;
// create a class in the header (also creates header if not existing)
var pClass = pModel.AddClass("X", "test.h");
// add the constructor implementation in the cpp file
pClass.AddFunction("X", EnvDTE.vsCMFunction.vsCMFunctionConstructor, null,
0, EnvDTE.vsCMAccess.vsCMAccessDefault, "test.cpp");
This correctly generates the following code in the test.h
file:
class X
{
X(void);
};
And the following test.cpp
file:
#include "test.h"
X::X(void)
{
}