繁体   English   中英

使用 C++ 从 .dll 调用带有默认参数的函数

[英]Call function with default argument from .dll with c++

我在 .dll 的 Header 中定义了一个函数

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

在 .cpp 文件中,它的定义如下:

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

我使用以下代码从另一个 C++ 代码调用这个 .dll:

#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);

}

这样,如果我输入"jj"参数,该函数就会起作用。 然而,由于它被定义为 .dll 中的标准输入,它也应该在没有它的情况下工作,但如果我尝试它不会编译。 有没有办法在从 .dll 加载函数的过程中声明"jj"参数具有标准输入值?

尝试使用Resultado(A,B, ans1);进行编译Resultado(A,B, ans1); 产生以下错误:

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

默认参数

只允许在函数声明的参数列表中

如果您不想默认标头中的参数,您可以通过重载函数来完成您想要做的事情:

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); }

作为额外的评论,请通过常量引用传递vector s,因为通过值传递会导致潜在的昂贵的复制成本。

尝试将标准参数值添加到函数指针类型声明中:

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

默认参数值是函数签名的一部分,编译器不会将其放入函数代码中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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