c++ - 从一个类的 vector 中打印内容

标签 c++

我想打印我类(class) Board 中 vector 的内容

class Board {
public:
    Board(const string &filename);


private:
    int numLines, numColumns;
    vector<Ship> ships;
    vector <vector <int> > board;

 };

船级:

class Ship {
public:
    Ship(char symb, PositionChar pos, char ori, unsigned int tam, unsigned int cor);

 private:

    char symbol;
    PositionChar position;

    char orientation;
    unsigned int size;
    unsigned int color;
    string status;


 };

为什么我不能做这样的事情?

cout << b1.ships[1].color << endl;

只是为了测试一下

最好的问候

最佳答案

Why I can't do something like this?

cout << b1.ships[1].color << endl;

因为 shipsBoard 的私有(private)成员,因此无法从类本身外部访问。

关于c++ - 从一个类的 vector 中打印内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30113438/

相关文章:

c++ - 为什么不为动态加载的 Qt 插件类调用析构函数?

Android JNI - 'raw' 函数编译失败

c++ - boost multi_index 示例 : error: invalid operands to binary expression

C++访问另一个类的数据成员

c++ - 如何将现有的 CMake git repo 作为子模块包含在我自己的 CMake 项目中?

c++ - 使用 Xojo 进行跨平台开发有哪些妥协?

C++ 异常处理 : defining exception as object

c++ - 使用 QServiceManager 未收到信号

c++ - 模板 self 友元

c++ - unique_ptr < 0 或者小于运算符做什么?