简体   繁体   中英

Why doesn't my object declaration call the default constructor?

This is my first time attempting to properly separate class declarations into header filers and their implementation into cpp files and I'm not sure whats going wrong. I'm attempting to create a Deck object in my main() which I assume would call my default constructor from the cpp file, but nothing is happening. My break points show the object is created by its completely devoid of anything I have in the default constructor. In fact the breakpoints I set inside the default constructor say that my current code does not reach them. Thanks for the help!

#include <iostream>
#include "Deck.h"
#include "Dealer.h"
#include "GameManager.h"
#include"Player.h"
#include "Card.h"



int main()
{
    Deck blackJackDeck;
    blackJackDeck.showDeck();
    Dealer blackJackDealer(blackJackDeck);
    Player blackJackPlayer;


    GameManager blackJackManager(blackJackPlayer, blackJackDealer);
    blackJackManager.setUpGame();
    std::cout << "ready?";
};

I believe my code is creating a deck object named blackJackDeck which will call its default constructor. Header file is below.

#pragma once
#include "Card.h"
#include "Node.h"
#include <vector>
#include <stack>

class Deck
{
public:
    Deck()
    {

    }
    void showDeck()
    {

    }

    void addCard(Card newCard)
    {

    }

    void print()
    {

    }

    void removeCard()
    {

    }

    Card popCard()
    {

    }

    void peekTop()
    {

    }

    bool isEmpty()
    {

    }


private:
    std::stack<Card, std::vector<Card>> shuffledDeck;
    std::vector<Card> deck;

};

cpp implementation is below

#include "Card.h"
#include "Node.h"
#include  <vector>
#include <stack>
#include <algorithm>
#include <random>
#include <time.h>


class Deck
{
public:
    Deck()
    {
        std::default_random_engine randomEngine{ std::random_device{}() };

        Card wild(1, 2);

        for (int i = 1; i <= 4; i++)
        {
            for (int j = 1; j <= 13; j++)
            {
                deck.push_back(wild);           
            }
        }

        std::cout << "test";

        std::shuffle(deck.begin(), deck.end(), randomEngine);

        for (Card k : deck)
        {
            shuffledDeck.push(k);
        }
    }

    void showDeck()
    {
        for (Card i : deck)
        {
            i.fullCard();
        }
    }


    void addCard(Card newCard)
    {
        shuffledDeck.push(newCard);
    }

    void print()
    {
        //todo
    }

    void removeCard()
    {
        //todo
    }

    Card popCard()
    {
        shuffledDeck.pop();
    }

    void peekTop()
    {
        shuffledDeck.top();
    }

    bool isEmpty()
    {
        if (shuffledDeck.size() == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }


private:
    std::stack<Card, std::vector<Card>> shuffledDeck;
    std::vector<Card> deck;
};

I expected the deck and Shuffled deck variables to be filled with Card objects but both have nothing in them.

The reason is that inside your header file you opened the curly braces on your function. The compiler considers this to be a complete function and does not need to go to the .cpp file where you have actually done the implementations. In order to correct this rewrite the functions in your header from:

Deck()
    {
    }

To:

Deck();

This applies to all the functions in your header file. Once you do this the default constructor will get called.

Also in your .cpp file you should not be making the Deck class again. Instead of this you should use the name of the class and the unary scope operator. I will give you an example with your default constructor:

You should just have this in your .cpp:

Deck::Deck()
{
   // code goes here
}

Instead of

class Deck
{
public:
   Deck()
   {
      // code goes here
   }
}

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