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
2
  • 1
    "it do not compile" - and you didn't think that posting the compile errors would be relevant? Apr 6, 2018 at 12:13
  • My bad I’ll do that in a second Apr 6, 2018 at 12:14

2 Answers 2

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.

6
  • So I need to change the .dll, is that? Sorry did not get where it would be inputted. Apr 6, 2018 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. Apr 6, 2018 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? Apr 6, 2018 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." Apr 6, 2018 at 13:04
  • 1
    If you're using a C interface, not only would the overload not be allowed, but the defaulting would not be allowed either: stackoverflow.com/q/9185429/2642059 Nor would the reference, or vectors in your function definition. So I'd start with the statement that you are not actually using a "C" interface :( Apr 6, 2018 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.

1
  • did not work, I got: error C2383: 'f_funci': default-arguments are not allowed on this symbol Apr 6, 2018 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.