使用C ++从.dll调用带有默认参数的函数 [英] Call function with default argument from .dll with c++

查看:279
本文介绍了使用C ++从.dll调用带有默认参数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

in the .cpp file it is defined as follows:

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

我正在使用以下代码从另一个C ++代码中调用此.dll:

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

}

这样,如果我输入 jj 参数。但是,由于它在.dll中被定义为标准输入,因此在没有它的情况下也应该可以工作,但是如果我尝试不编译。在从.dll加载函数的过程中,有没有一种方法可以声明 jj 参数具有标准输入值?

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?

尝试使用 Resultado(A,B,ans1); 生成以下错误:

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

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


推荐答案

默认参数


仅在函数声明的参数列表中允许使用

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

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

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

这篇关于使用C ++从.dll调用带有默认参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆