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

时间:2021-02-18 15:56:10

标签: c++ inheritance

我有一个带有派生类 Container 的基类 Player_Inventory。只能有一个 Player_Inventory,因此如果出于某种原因创建了第二个 Player_Inventory,我的代码会抛出异常。

我遇到的问题是我的代码没有通过我的测试,因为它甚至在应该是 number 类的第一个构造时抛出异常。我已经调试了代码,发生了两件我不太明白的事情 - REQUIRE 属性没有被调试器跟踪(至少不在 VSC 的 GUI 中),而且似乎在点击之后第一个 #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;} }; 语句,构造函数被再次调用,从而触发异常。

有人可以帮忙吗?


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

我修改后的代码如下:

容器.h

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

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

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

#include "containers.h"

class Player_Inventory : public Container {

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

player_inventory.h

#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;

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

test_file.cpp

   error: Can not find Rust compiler
    
    ----------------------------------------
Command "/usr/local/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-9vs5iiqi/cryptography/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-q8luryeh/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-9vs5iiqi/cryptography/

  error: Can not find Rust compiler
  
  ----------------------------------------
  Failed building wheel for cryptography
  Running setup.py clean for cryptography
Successfully built browser-cookie3 docopt pysftp pyhive pysocks pyyaml s3fs maxminddb pyaes pbkdf2 future sasl thrift thrift-sasl
Failed to build cryptography

1 个答案:

答案 0 :(得分:1)

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

Player_Inventory(std::string n, std::string desc) : Container(n, desc) {
}
相关问题