2

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
Patrick Machado
  • 61
  • 1
  • 11

2 Answers2

2

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.

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • So I need to change the .dll, is that? Sorry did not get where it would be inputted. – Patrick Machado Apr 06 '18 at 12:41
  • @PatrickMachado Yes your .dll and corresponding header will need to be changed to either default the original `calculo` function, or to overload the `calculo` function. – Jonathan Mee Apr 06 '18 at 12:49
  • So there is no explicit way to define that after that the .dll is defined? Because if the function have to be overloaded seems useless that c++ allows us to define a default argument. Because as I understood if I take the second line of your answer it would only allow me to use the standard value to jj, am I right? – Patrick Machado Apr 06 '18 at 12:54
  • I am understanding your statement to read: "There isn't a way to default a variable without changing a .dll's header?" If that is in fact your question, then the answer is, "That's correct, if you want to change the number of arguments a function requires there *must* be a change to the header." – Jonathan Mee Apr 06 '18 at 13:04
  • Tried to define the default arguments in the header however still getting: error C2383: 'f_funci': default-arguments are not allowed on this symbol error C2198: 'f_funci': too few arguments for call. the overload is not an option once that I am externing in "C" those functions – Patrick Machado Apr 06 '18 at 13:31
  • 1
    If you're using a C interface, not only would the overload not be allowed, but the defaulting would not be allowed either: https://stackoverflow.com/q/9185429/2642059 Nor would the reference, or `vector`s in your function definition. So I'd start with the statement that you are not actually using a "C" interface :( – Jonathan Mee Apr 06 '18 at 13:43
-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.