繁体   English   中英

为什么我的 class 构造函数被调用两次?

[英]Why is my class constructor being called twice?

我有一个带有派生 class Player_Inventory的基本 class Container 只有一个Player_Inventory ,所以如果由于某种原因创建了第二个,我的代码会引发异常。

我遇到的问题是我的代码没有通过我的测试,因为它即使在应该是Player_Inventory class 的第一个构造上也会引发异常。 我已经调试了代码,并且发生了两件我不太明白的事情 - 调试器没有跟踪number属性(至少在 VSC 的 GUI 中没有),而且似乎在点击第一个REQUIRE语句之后,再次调用构造函数,从而触发异常。

任何人都可以帮忙吗?


重写我的构造方法后,我仍然遇到类似的错误。

我修改后的代码如下:

容器.h

#include<iostream>
#include<string>
#include<vector>

class Item {    // Placeholder class for items
    public:
        std::string name;
        Item(std::string n) : name{n} {};
};

class Container {
    protected:
        std::string name;
        std::string description;
        std::vector<Item> contents;

    public:
        Container(std::string, std::string);
        std::string get_name() {return name;}
        std::string get_description() {return description;}
        std::vector<Item> get_contents() {return contents;}
};

container.cpp(此文件中定义了更多方法,此处未使用)

#include<iostream>
#include<string>
#include "containers.h"

Container::Container(std::string n, std::string desc) : name{n}, description{desc} {};

player_inventory.h

#include "containers.h"

class Player_Inventory : public Container {

    public:
        static int number;
        Player_Inventory(std::string, std::string);
};

player_inventory.cpp

#include<iostream>
#include<stdexcept>
#include "player_inventory.h"

Player_Inventory::Player_Inventory(std::string n, std::string desc): Container(n, desc) {
    number += 1;
    if (number > 1){
        throw std::invalid_argument("You can only have one inventory!");
    }
};

int Player_Inventory::number = 0;

测试文件.cpp

#include "../lib/Catch2/catch.hpp"
#include "player_inventory.h"
#include<iostream>
#include<string>
#include<vector>

SCENARIO("A player can have an inventory.") {

    WHEN("A player inventory  is created.") {
        Player_Inventory myInventory("My Inventory", "Inventory for the player");

        THEN("The created inventory has the correct attribute values.") {
            REQUIRE(myInventory.get_name() == "My Inventory");
            REQUIRE(myInventory.get_description() == "Inventory for the player");
            REQUIRE(myInventory.get_contents().empty());
        }  // The code works fine when only up to here is included

        AND_THEN("Only one player inventory can exist.") { // as soon as this line is included it tries to create another player_inventory object, causing the fail
            REQUIRE_THROWS((Player_Inventory myOtherInventory("Second Inventory", "Testing for another one"))); // These two lines were not included but I've included them here as this is the test I wanted to run
            REQUIRE(myInventory.get_number() == 1);
        }
    }
}

不确定是否相关,但这就是您应该调用 Base 构造函数的方式:

Player_Inventory(std::string n, std::string desc) : Container(n, desc) {
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM