简体   繁体   中英

Return vector<string> by reference

I have a SWIG class that receives an vector of strings - vector<string> (CSCHSwig), this class uses a. LIB another project that returns another vector of strings by reference.

CSCHSwig.cpp

#include CSCHSwig.h
vector < string> CSCHSwig::CSwig(vector < string> a_InputArgs){

    vector < string> a_OutputArgs;
    int resposta = ClassLib->SendRequest(a_InputArgs, a_OutputArgs);

    return a_OutputArgs
}

CSCHSwig.h

#include < string>
#include < vector> 
using namespace std;

class CSCHSwig { public:
    CSCHSwig();
    virtual ~CSCHSwig();
    vector <string> CSwig(const vector < string> a_InputArgs);
}

CSCHSwig.i

/* File : CSCHSwig.i */

%module CSCHSwig

%{
#include "..\..\..\Incl\CSCHSwig.h"
%}

%include <std_string.i>
%include <std_vector.i>
%include "typemaps.i"


namespace std {
    %template(a_OutpuArgs) vector < string>; 
}


%include "..\..\..\Incl\CSCHSwig.h"

An example of ClassLib: ClassLib.cpp

int ClassLib::SendRequest(const vector < string>& a_InputArgs, vector < string>& a_OutputArgs, {
    vector < string> Vector;

    Vector.push_back("pReturnStatus");
    Vector.push_back("1");

    a_OutputArgs = Vector;

    return 1;
}

ClassLib.h

class ClassLib
{
public:
    int SendRequest(const vector < string>&  a_InputArgs, vector < string>& a_OutputArgs);
}

I've tested the SWIG class and it is working perfectly, I call CSwig method from python passing a list of strings. The problem is when the CSwig method calls the method SendRequest ClassLib. int resposta = ClassLib->SendRequest(a_InputArgs, a_OutputArgs);

Execution is terminated, returns no error. The tests I made the "mistake" happens when the a_OutputArgs argument is handled within the method and returns an array of strings. Maybe I need to put something in the .i file for this to work.

I found the problem. I use MS Visual Studio 2008 and the setting was different projects. In Project Properties> General> Use of MFC set:

Use MFC in a Shared DLL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM