【问题标题】:Passing C++ string to a c# function将 C++ 字符串传递给 C# 函数
【发布时间】:2015-11-02 16:54:08
【问题描述】:

我将 Microsoft 的断言类包装在 c# 中,并希望在 c++ 中使用该代码。我遇到的问题必须处理信息字符串,我不太确定如何将 c++ 字符串传递给 c# 方法。两者都是托管的。

//c# Assert wrapper
static public bool AreEqual<T>(T expected, T actual, string info){
    //...
    WriteLine(info);
    //...
}

从 C++ 调用

//c++
void someCppFunc(){
    long expected = 2;
    long actual = 2;
    Assert::AreEqual<long>(expected, actual, L"Some info message");
}

但是我得到的错误说它没有匹配的功能。我该怎么办?

【问题讨论】:

  • IMO,您最好使用原生 C++ 单元测试库,如 Boost.Test、google test 或 Catch

标签: c# c++ string interop


【解决方案1】:

只需传递一个托管字符串而不是原生 C++ 字符串:

//c++/clr
void someCppFunc(){
    long expected = 2;
    long actual = 2;
    Assert::AreEqual<long>(expected, actual, gcnew System::String(L"Some info message"));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-09
    • 2019-09-28
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    相关资源
    最近更新 更多