Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将一个类的对象列表传递到另一个类c++;_C++ - Fatal编程技术网

C++ 将一个类的对象列表传递到另一个类c++;

C++ 将一个类的对象列表传递到另一个类c++;,c++,C++,编辑:问题修复,刚刚意识到我可能不想我的完整代码在线,因为这可能会触发我的unis反plaigurism,即使它是我自己的代码。。。不允许我删除post不确定您的ship.h是什么(您需要发布所有内容),但是创建一些简单的版本,它似乎可以正常工作: #include<string> #include<iostream> // to use input and output streams #include<memory> // to use smart po

编辑:问题修复,刚刚意识到我可能不想我的完整代码在线,因为这可能会触发我的unis反plaigurism,即使它是我自己的代码。。。不允许我删除post

不确定您的
ship.h是什么(您需要发布所有内容),但是创建一些简单的版本,它似乎可以正常工作:

#include<string>
#include<iostream>  // to use input and output streams
#include<memory> // to use smart pointers
#include<vector> // to use vectors

struct Ship{};
struct Destroyer : Ship {};

class Board{
private:
    std::unique_ptr<char[]> bdata; // to hold the data about the board
    int rows, columns;
    std::vector<Ship*> shipslist;
public:
    Board(std::vector<Ship*> &list); // Constructor
    ~Board() {} // Destructor
};

Board::Board(std::vector<Ship*> &list) : rows{ 10 }, columns{ 10 }, shipslist{list}
{
    // irrelevant code here
}

int main()
{
    // Make vector of Ships
    std::vector<Ship*> ships;
    ships.push_back(new Destroyer()); // simplified constructor call
    // (Destroyer is a derived class of the abstract base class "Ship")
    Board BoardConfig(ships); // Here is where i want to create a board using a list of ships i have made
return 0;
}
#包括
#包含//以使用输入和输出流
#包含//以使用智能指针
#包含//以使用向量
结构船舶{};
结构驱逐舰{};
班级委员会{
私人:
std::unique_ptr bdata;//保存有关板的数据
int行、列;
std::向量shipslist;
公众:
线路板(标准::向量和列表);//构造函数
~Board(){}//析构函数
};
Board::Board(std::vector&list):行{10},列{10},发货列表{list}
{
//不相关的代码在这里
}
int main()
{
//制造船只的航向
病媒船;
ships.push_back(new Destroyer());//简化的构造函数调用
//(驱逐舰是抽象基类“船”的派生类)
Board BoardConfig(ships);//在这里,我想使用我制作的船舶列表创建一个板
返回0;
}

live:

类不是对象。您不在列表中传递类,而是传递对象。类是描述,对象是所描述事物的实例。您正在构建Board并使用初始值设定项列表来初始化成员:行、列和shipslist,它可能试图复制Ships的向量及其内容?。但由于Ship是一个抽象类,它无法按原样复制其内容。尝试重写您的构造函数,只需将每个Ship作为指针/引用进行复制。谢谢您的帮助。我已经用完整的代码编辑了我的帖子,以防出现更明显的问题。