将std :: vector从托管的VS测试项目传递到托管的Dll

时间:2019-03-08 09:18:00

标签: c# c++ visual-studio-2017 clr vstest.console.exe

我正在使用托管VS Test项目编写单元测试。
我有一个托管的(CLI / C ++)代码,如下所示

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

我已经将上述项目构建为DLL。
在测试项目中,我参照上面生成的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);
        }
    }
}

在上述代码中,我省略了所有其他include和using语句,例如 using namespase System include
现在,当我尝试构建项目时,出现以下错误

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> > *'

我什至尝试 c1-> fillVec(&temp); 来匹配错误注释中给出的参数,但还是没有运气。

有人可以帮助我解决瓶颈吗?

0 个答案:

没有答案