Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

c++ cli - Visual Studio 2019 Class does not have a correct Copy Constructor C++

I'm working on a project for uni and I was thinking about a simple game where you construct ships and battle with a fleet of 6 of them using visual studio code but when I try to constuct an object i always get an error of an incorrect copy constructor in "Ship" class. I humbly ask for help.(the change in this->is because i was trying everything to get it to work)

String^ tekst = MyForm2::textBox1->Text;
    int FP = int::Parse(MyForm2::textBox2->Text);
    int Armor = int::Parse(MyForm2::textBox3->Text);
    int Speed = int::Parse(MyForm2::textBox4->Text);
    int AA = int::Parse(MyForm2::textBox5->Text);
    int special= int::Parse(MyForm2::textBox6->Text);
    int hp = 15;

    if (radioButton1->Checked == true)
    {
        Ship s_1 = Ship (tekst, FP, hp, Armor, Speed, AA);

    }
ref class Ship
{
public:
    String^ nazwa;
    int FP;
    int HP;
    int ARMOR;
    int SPEED;
    int AA;
    Ship() {};
    Ship(String^ new_nazwa, int new_fp, int new_hp, int new_armor, int new_speed,int new_aa)
    {
        nazwa = new_nazwa;
        FP = new_fp;
        HP = new_hp;
        ARMOR = new_armor;
        SPEED = new_speed;
        AA = new_aa;
    };

};


ref class Destroyer : public Ship

{
public:
    Destroyer() {};
    Destroyer(String^ new_nazwa, int new_fp, int new_hp, int new_armor, int new_speed, int new_torp,int new_aa)
    {
        nazwa = new_nazwa;
        FP = new_fp;
        HP = new_hp;
        ARMOR = new_armor;
        SPEED = new_speed;
        TORP = new_torp;
        AA = new_aa;
    };
    int TORP;

};
ref class Battleship : public Ship

{
public:
    Battleship(String^ new_nazwa, int new_fp, int new_hp, int new_armor, int new_speed, int new_sec_fp,int new_aa)
    {
        this->nazwa = new_nazwa;
        this->FP = new_fp;
        this->HP = new_hp;
        this->ARMOR = new_armor;
        this->SPEED = new_speed;
        this->Sec_FP = new_sec_fp;
        this->AA = new_aa;
    };
    int Sec_FP;
};
ref class Carrier : public Ship

{
public:
    Carrier(String^ new_nazwa, int new_fp, int new_hp, int new_armor, int new_speed, int new_torp,int new_aa)
    {
        this->nazwa = new_nazwa;
        this->FP = new_fp;
        this->HP = new_hp;
        this->ARMOR = new_armor;
        this->SPEED = new_speed;
        this->TORP = new_torp;
        this->AA = new_aa;
    };
    int TORP;

};
ref class Cruiser : public Ship

{
public:
    Cruiser(String^ new_nazwa, int new_fp, int new_hp, int new_armor, int new_speed, int new_aa)
    {
        this->nazwa = new_nazwa;
        this->FP = new_fp;
        this->HP = new_hp;
        this->ARMOR = new_armor;
        this->SPEED = new_speed;
        this->AA = new_aa;
    };

};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ship s_1 = Ship (tekst, FP, hp, Armor, Speed, AA);

This line attempts to create an instance of the reference type Ship on the stack with a syntax that invokes the copy constructor. This is allowed since VS2005, but with several caveats (quoted from C++ Stack Semantics for Reference Types):

When you create an instance of a reference type using stack semantics, the compiler does internally create the instance on the garbage collected heap (using gcnew). [...] The compiler will not generate a copy constructor for a reference type.

Following are possible ways to get it to work (without discussing the merits of creating ref types with stack semantics in general, which is covered at the the previous link).

  • Do not use stack semantics. Instead:

    Ship ^s_1 = gcnew Ship(tekst, FP, hp, Armor, Speed, AA);
    
  • Use stack semantics with direct construction, instead of copy-construction:

    Ship s_1(tekst, FP, hp, Armor, Speed, AA);
    
  • Use the code as posted, but define a copy constructor for the ref class Ship:

    Ship(const Ship %other)
    {
      nazwa = other.nazwa;
      /* ... */
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...