移动语义:无法将参数1从'std :: string'转换为'int'

时间:2019-09-01 15:02:18

标签: unique-ptr

我想将t_name传递给对象,并使用名称和id_runner增加的cout对象man(1、2、3、4)(向量中的类型为unique_ptr,向量的返回类型为unique_ptr)。我已经引用了许多答案,但是仍然没有正确的代码。 错误报告:'MC :: MC(MC &&)':无法将参数1从'std :: string'转换为'int'。很高兴帮助我纠正运行程序,非常感谢!

#include<iostream>
#include<string>
#include<vector>
#include<memory>
using namespace std;
//=============================================================
class MR
{
private:
    string name_MR;
    int id_MR; 
    int runTime_MR;
public:
    MR() : name_MR("empty"), id_MR(0), runTime_MR(0) {}

    //GET - SET NAME
    string getName_MR() const { return name_MR; }
    void setName_MR(string t_name_MR) { name_MR = t_name_MR; }

    //GET - SET ID
    int getID_MR() const { return id_MR; }
    void setID_MR(int t_id_MR) { id_MR = t_id_MR; }

    //GET - SET RUNTIME
    int getRunTime_MR()const { return runTime_MR; }
    void setRunTime(int t_runTime_MR) { runTime_MR = t_runTime_MR; }

        ~MR() { cout << "Destructored" << endl; }
};
//=============================================================
class MC
{
private:
    int id_Runner;
public:
    MC() : id_Runner(0) {}
    MC(int t_id_Runner) : id_Runner(t_id_Runner){}

        int upID() { return id_Runner; }

    vector<unique_ptr<MC>> runnerList;

    // std::unique_ptr<Test> ptr = std::unique_ptr<Test>(new Test()); 
    // or std::unique_ptr<Test> ptr(new Test());
    //std::unique_ptr<Test> ptr = std::make_unique<Test>();

    unique_ptr<MC> c_MC( string t_name)
    {
        id_Runner++;
        //unique_ptr<MC> runner = make_unique<MC>(t_name);//allocate
        auto runner = make_unique<MC>(t_name);//allocate
        runnerList.push_back(std::move(runner));
        return runner;
    }
};
//=============================================================
int main()
{

   {
        MC mr_MC;
        auto man1 = mr_MC.c_MC("name1");
        auto man2 = mr_MC.c_MC("name2");
        auto man3 = mr_MC.c_MC("name3");
        auto man4 = mr_MC.c_MC("name4");

       for (auto& man : mr_MC.runnerList)
       {
    cout << "\nman has id " << man->upID() << mr_MC->getName_MR()<< endl;
       }
    }
    system("pause");
    return 0;
}

/*expect print: 
1 name1
2 name2
3 name3
4 name4*/

0 个答案:

没有答案