Call function with default argument from .dll with

2020-04-17 03:53发布

问题:

I have a function defined in the Header of my .dll

void calculo(vector<double> A, vector<int> B, double &Ans1, double jj);

in the .cpp file it is defined as follows:

void calculo(vector<double> A, vector<int> B, double &Ans1, double jj = 36.5);

I am calling this .dll from another c++ code using the following code:

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include "TEST_DLL.h"


typedef void(_stdcall *f_funci)(vector<double> A, vector<int> B, double &Ans1, double jj);

int main()
{

vector<double> A;
vector<int> B;
double ans1;
double teste;

HINSTANCE hGetProcIDDLL = LoadLibrary(L"MINHA_DLL.dll");
    if (!hGetProcIDDLL) {
        std::cout << "could not load the dynamic library" << std::endl;
        return EXIT_FAILURE;
    }


f_funci Resultado = (f_funci)GetProcAddress(hGetProcIDDLL, "calculo");
    if (!Resultado) {
        std::cout << "could not locate the function" << std::endl;
        return EXIT_FAILURE;
    }

Resultado(A,B, ans1, teste);

}

This way the function works if I input the "jj" parameter. However as it is defined as an standard input in the .dll it should work also without it but if I try it do not compile. Is there a way that I could declare in the procedure of loading the function from the .dll that the "jj" parameter have a standard input value?

trying to compile using Resultado(A,B, ans1); generates the following error:

error C2198: 'f_funci': too few arguments for call

回答1:

Default arguments:

Are only allowed in the parameter lists of function declarations

If you don't want to default the argument in the header you could accomplish what you're trying to do by overloading the function:

void calculo(const vector<double>& A, const vector<int>& B, double &Ans1, const double jj);
void calculo(const vector<double>& A, const vector<int>& B, double &Ans1) { calculo(A, B, Ans1, 36.5); }

As a bonus comment, please pass vectors by constant reference as passing by value incurs the potentially expensive copy cost.



回答2:

Try add standard parameter value to function pointer type declaration:

typedef void(_stdcall *f_funci)(vector A, vector B, double &Ans1, double jj = 36.5);

Default argument value is part of function signature, compiler does not put it into function code.



标签: c++ function dll