C ++ / cli to c ++:错误与“void(__stdcall *)(int i,std :: string str)”不兼容。 [英] C++/cli to c++: Error s incompatible with "void (__stdcall *)(int i, std::string str)"

查看:380
本文介绍了C ++ / cli to c ++:错误与“void(__stdcall *)(int i,std :: string str)”不兼容。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++ / cli的新手,请原谅我的愚蠢的问题或核心错误:



我的C ++ / CLI类库被C#调用只有一个方法:



ThreadsCppWrapper.h

 命名空间命名空间ThreadsCppWrapper {

public ref class ThreadsCppWrapperClass
{
public:
ThreadsCppWrapperClass(); // Constructor

System :: String ^ mytrainOp(System :: MulticastDelegate ^ callbackfunction);
private:
MyThreadsCpp :: MyThreadsCppClass * ptr;
};
}

ThreadsCppWrapper.cpp

  System :: String ^ ThreadsCppWrapper :: ThreadsCppWrapperClass :: mytrainOp(System :: MulticastDelegate ^ callbackfunction){

System :: String ^ toReturn = gcnew System: :串();

msclr :: interop :: marshal_context context;
System :: IntPtr intPtr = System :: Runtime :: InteropServices :: Marshal :: GetFunctionPointerForDelegate(callbackfunction);
if(intPtr!= System :: IntPtr :: Zero)
{
myUpdateProgress functPtr = static_cast< myUpdateProgress>(intPtr.ToPointer());
string stdstringResult = ptr-> train(functPtr); // Error Here // ERRORLINE

// toReturn = context.marshal_as< System :: String ^>(stdstringResult);
}
else
{
System :: Console :: WriteLine(Error:Could not cast delegate !!);
}
return toReturn;
}

从c ++ / cli类调用的C ++类是:



MyThreadsCpp.h

 命名空间MyThreadsCpp {

class MyThreadsCppClass
{
public:
MyThreadsCppClass();
string train(void(CALLBACK * myUpdateProgress)(int i,string str));
};

}

MyThreadsCpp.cpp

  string MyThreadsCpp :: MyThreadsCppClass :: train(void(CALLBACK * myUpdateProgress)(int i,string str)){

double sum = 0.0;
long max = 100 * 1000;
int perc = 0 ;;
string trainstat =训练状态:开始训练...;
ostringstream oss;

myUpdateProgress(perc,trainstat);
for(long n = 0; n sum + = 4.0 * pow(-1.0,n)/(2 * n + 1.0)
int rem = n%(max / 10);
if(rem == 0){
perc = 100 * n / max;
cout<< perc<< %< endl;
/ *报告完成状态* /
oss< 训练状态:< ;
myUpdateProgress(perc,oss.str());
}
}
cout<< 100%<< endl;
ostringstream oss;
oss<< Result =<<和;
return oss.str();
}

它编译得很好。



Intellisense包装类中的错误ERRORLINE:
1myUpdateProgress类型的参数与ThreadsCppWrapper.cpp中的类型为void(__stdcall *)(int i,std :: string str)的参数不兼容

专家,请帮助。

解决方案

c> std :: string 和 System :: String ^ 。您必须将它们转换为一种类型。强制转换类型或函数指针/回调将不会工作 - 如果不是编译时,它将在运行时断开。


I am new to C++/cli so please excuse me for silly questions or core mistakes:

My C++/CLI class Library that is called by C# has only one method:

ThreadsCppWrapper.h

namespace namespace ThreadsCppWrapper {

public ref class ThreadsCppWrapperClass
    {
        public:
        ThreadsCppWrapperClass(); // Constructor

        System::String^ mytrainOp(System::MulticastDelegate^ callbackfunction);
    private:
        MyThreadsCpp::MyThreadsCppClass* ptr;
    };
}

ThreadsCppWrapper.cpp

System::String^ ThreadsCppWrapper::ThreadsCppWrapperClass::mytrainOp(System::MulticastDelegate^ callbackfunction){

System::String^ toReturn = gcnew System::String("");

msclr::interop::marshal_context context;
System::IntPtr intPtr = System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(callbackfunction);
if (intPtr != System::IntPtr::Zero)
{
    myUpdateProgress functPtr = static_cast<myUpdateProgress>(intPtr.ToPointer());
    string stdstringResult = ptr->train(functPtr); // Error Here   // ERRORLINE

    //toReturn = context.marshal_as<System::String^>(stdstringResult);
}
else
{
    System::Console::WriteLine("Error: Could not cast delegate!!");
}
return toReturn;
}

The C++ class that is called from c++/cli class is:

MyThreadsCpp.h

namespace MyThreadsCpp{

class MyThreadsCppClass
{
public:
    MyThreadsCppClass();
    string train(void(CALLBACK *myUpdateProgress)(int i, string str));
};

}

MyThreadsCpp.cpp

string MyThreadsCpp::MyThreadsCppClass::train(void(CALLBACK *myUpdateProgress)(int i, string str)){

double sum = 0.0;
long max = 100 * 1000;
int perc = 0;;
string trainstat = "Training Status: Starting Training...";
ostringstream oss;

    myUpdateProgress(perc, trainstat);
for (long n = 0; n < max; n++){
    sum += 4.0*pow(-1.0, n) / (2 * n + 1.0);
    int rem = n % (max/10);
    if (rem == 0){
        perc = 100 * n / max;
        cout << perc << "%" << endl;
        /* report completion status */
        oss << "Training Status: " << perc;
        myUpdateProgress(perc, oss.str());
    }
}
cout << "100%" << endl;
ostringstream oss;
oss << "Result = " << sum;
return oss.str();
}

It compiles fine.

Intellisense Error in wrapper class ERRORLINE: 1 argument of type "myUpdateProgress" is incompatible with parameter of type "void (__stdcall *)(int i, std::string str)" in ThreadsCppWrapper.cpp

Experts, Please help.

解决方案

You cannot use std::string and System::String^ interchangeably. You must convert them to one type. Forceful conversion of type or the function-pointer/callback won't work - it will break at runtime, if not compile time.

这篇关于C ++ / cli to c ++:错误与“void(__stdcall *)(int i,std :: string str)”不兼容。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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