문제

This piece of code is what i want to generate (c++)

X::X()
{
}

However, the AddFunction call bellow generates a function with return type (source being a FileCodeModel representing a cpp source file):

source.AddFunction("X::X" , vsCMFunction.vsCMFunctionConstructor, null, -1,vsCMAccess.vsCMAccessDefault);

Replacing the null with any of these return types make no difference:

"X"

vsCMTypeRef.vsCMTypeRefVoid

vsCMTypeRef.vsCMTypeRefCodeType

vsCMTypeRef.vsCMTypeRefOther

All of these results in a function with return type like below, even tough vsCMFunctionConstructor is specified:

int X::X()
{
}

Using the FileCodeModel interface, how can i generate a simple constructor, that is a function without return type?

Here is the most minimal code to recreate my problem.

도움이 되었습니까?

해결책

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)
{
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top