简体   繁体   中英

Unable to print vector object data member values in C++

I am creating a snakes and ladders game. For now, I have created two classes board and ladders . The board is responsible for printing the board and the ladder is responsible for all ladder functionality.
Here is my ladder.h class:

#ifndef LADDER_H
#define LADDER_H

#include <iostream>

using std::ostream;

class Ladder
{
private:
    unsigned int m_Ladder_bottom, m_Ladder_top, m_Ladder_number;
    static int m_Ladder_counter;
    const static int m_Ladder_ladderBottom[6];
    const static int m_Ladder_ladderTop[6];
public:
    // Constructor
    Ladder() {
            m_Ladder_bottom = m_Ladder_ladderBottom[m_Ladder_counter - 1];
            m_Ladder_top = m_Ladder_ladderTop[m_Ladder_counter - 1];
            m_Ladder_number = m_Ladder_counter;
            ++m_Ladder_counter;
    }

    friend ostream &operator << (ostream &os, const Ladder &ladder) {
            os << "Ladder Number: " << ladder.m_Ladder_number << "\nLadder Top Position: " << 
            ladder.m_Ladder_top << "\nLadder Bottom Position: " << ladder.m_Ladder_bottom;
            os << "\n-------------------------------------------------------------------------------- 
            ------------------\n";
            return os;
    }
};

int Ladder::m_Ladder_counter = 1;
const int Ladder::m_Ladder_ladderBottom[6] = { 4, 12, 14, 22, 41, 54 };
const int Ladder::m_Ladder_ladderTop[6] = { 56, 50, 55, 58, 79, 88 };

#endif // !LADDER_H

Here is my board.h class:

#ifndef BOARD_H
#define BOARD_H

#include <vector>
#include "Ladder.h"

using std::cout;
using std::vector;
using std::ostream;

class Board
{
private:
    vector<Ladder> m_Board_ladders;
public:
    Board() {
        m_Board_ladders.reserve(6);
    }

    void m_Board_PrintLadders() {
        vector<Ladder>::const_iterator iter;
        for (iter = m_Board_ladders.begin(); iter != m_Board_ladders.end(); ++iter) {
            cout << *iter << '\n';
        }
    }
};


#endif // !BOARD_H

In my main class when I create an object of type ladder , I am printing all the ladder locations successfully:

#include "Ladder.h"
#include <vector>

using std::cout;
using std::vector;

int main() {
    vector<Ladder> ladders(6);
    vector<Ladder>::const_iterator iter;
    for (iter = ladders.begin(); iter != ladders.end(); ++iter) {
        cout  << *iter;
    }

    system("pause");
}

Here is my output: https://i.stack.imgur.com/vtZZr.png

Although, when I create an object of type board and then try to print all the ladders (even tho I have the same exact code copy-pasted), it does not work? Here is my main class with board object:

#include "Board.h"

using std::cout;

int main() {
    Board board;
    board.m_Board_PrintLadders();

    system("pause");
}

Here is my output: https://i.stack.imgur.com/d2YXt.png



For the Ladders version you are creating a vector of 6 elements.

For the Boards version, you don't get any output because m_Board_ladders is empty when you construct the Board . Note that calling reserve doesn't actually change the size of a vector :

Board() {
    m_Board_ladders.reserve(6);  // still empty
}

If you want the vector to have 6 elements, you can do:

Board() : m_Board_ladders(6) {}

Here's a demo .

@ProgrammingRage : you have not initialised your Board.m_Board_ladders , you have just called the allocator with the reserve() command to book it some space.

You have to initialise m_Board_ladders = Ladder() once you reserve the space, so that the constructor for Ladder gets actually called.

Better still, do not declare the Board() constructor in your class at all, leave it to the compiler to synthesize it, and it will create it correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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