将std :: vector从托管VS测试项目传递给托管Dll [英] Pass std::vector from managed VS test project to managed Dll

查看:62
本文介绍了将std :: vector从托管VS测试项目传递给托管Dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用托管VS Test项目编写单元测试。  


我有一个托管(CLI / C ++)代码,如下所示

  命名空间    ClassLibrary1     {  
公开 参考 Class1
{
std
:: 向量 < int> * vec1 ;
公开
Class1 () {
这个 - > vec1 = std :: 向量 < int> ();
}
无效 fillVec std :: vector < int> inp {
std :: vector < int> :: iterator it = inp 开始 (); != inp 结束 (); ++ {
这个 - > vec1 - > push_back (* it );
}
}
}



我已将上述项目构建为DLL。 

在我的测试项目中,我编写了如下代码,参考上面生成的dll 。

  #using" ClassLibrary1.dll" as_friend   
命名空间 UnitTestDemoProjTwo
{
[ TestClass ]
公开 参考 UnitTest
{
ClassLibrary1 :: Class1 ^ c1 ;
公开
[ TestInitialize ()]
无效 MyTestInitialize ()
{
c1
= gcnew ClassLibrary1 :: Class1 ;
}
[ TestMethod ]
无效 TestMethod03 ()
{
std
:: 向量 < int> temp ;
temp
清除 ();
temp
push_back 12 );
temp
push_back 11 );
c1
- > clearVec (); //将在Class1中使用func清除向量
c1
- > fillVec temp );
断言 :: AreEqual c1 - > retSizeOfVec (), 2 );
}
}
}



我没有在上面的代码中显示所有其他包含和使用语句,例如  using namespase
System
,  include  。 

现在当我尝试构建项目时,我收到以下错误

  错误C2664     'void ClassLibrary1 :: Class1 :: fillVec(std :: vector< int,std :: allocator< int>> *)'   
无法转换参数
1 来自 'std :: vector< int,std :: allocator< _Ty>>' 'std :: vector< int,std :: allocator< int>> *'



我甚至试过  c1-> fillVec(& temp);  以匹配错误
条评论中给出的argement,仍然没有运气。



任何人都可以帮我解决瓶颈问题吗?



我还尝试从 project->添加引用;而不是#using,将ClassLibrary1的引用添加到UnitTestDemoProjTwo项目。现在错误将出现错误
C3767:'ClassLibrary1 :: Class1 :: fillVec':候选函数无法访问



我认为它无法通过DLL访问托管的func(),因为我传递的是非托管容器(std :: vector)。如何传递该向量需要帮助

解决方案

这似乎有效:



无效 fillVec(
IntPtr
inp0



{



  
const
自动 & inp = *( vector < int > *) inp0 。ToPointer();



  
...



}


 


用法:


c1-> fillVec(
IntPtr (& temp));


 


并使用项目参考代替使用#using


am writing a unit test using managed VS Test project. 

I have a managed (CLI/C++) code as shown below

namespace ClassLibrary1 {
   public ref class Class1
        {
        std::vector<int>* vec1;
    public:
            Class1() {
                this->vec1 = new std::vector<int>();
            }
    void fillVec(std::vector<int> inp) {            
                for (std::vector<int>::iterator it = inp.begin(); it != inp.end(); ++it) {
                    this->vec1->push_back(*it);
                }
        }
}

I had build the above project as DLL. 
In my test project I had made the code as below, referring to the above generated dll.

#using "ClassLibrary1.dll" as_friend
namespace UnitTestDemoProjTwo
{
[TestClass]
    public ref class UnitTest
    {
        ClassLibrary1::Class1 ^c1;
    public: 
[TestInitialize()]
        void MyTestInitialize()
        {
            c1 = gcnew ClassLibrary1::Class1;               
        }
[TestMethod]
        void TestMethod03()
        {
            std::vector<int> temp;
            temp.clear();
            temp.push_back(12);
            temp.push_back(11);
            c1->clearVec();//func in Class1 which will clear the vector
            c1->fillVec(temp);
            Assert::AreEqual(c1->retSizeOfVec(), 2);
        }
    }
}

I had not shown all the other includes and using statement in this above code, like using namespase System, and include 
Now when I am trying to build the project I am getting the below error

error C2664: 'void ClassLibrary1::Class1::fillVec(std::vector<int,std::allocator<int> > *)': 
    cannot convert argument 1 from 'std::vector<int,std::allocator<_Ty>>' to 'std::vector<int,std::allocator<int> > *'

I even tried c1->fillVec(&temp); to match the argement given in the error comments, still no luck.

Could anyone please help me out how to resolve the bottleneck?

I also tried adding the reference for ClassLibrary1 to UnitTestDemoProjTwo project from project->Add references; instead of #using. Now the error is coming as error C3767: 'ClassLibrary1::Class1::fillVec': candidate function(s) not accessible.

I think it is unable to access the managed func() through DLL, as I am passing unmanaged Containers(std::vector). need help in how to pass that vector.

解决方案

This seems to work:

void fillVec( IntPtr inp0 )

{

   const auto & inp = *(vector<int>*)inp0.ToPointer();

   . . .

}

 

Usage:

c1->fillVec( IntPtr(&temp) );

 

And use project references instead of #using.


这篇关于将std :: vector从托管VS测试项目传递给托管Dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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