cannot call member function void Deck::build_deck without object

Hi there,

this is my first post so sorry if i did it wrong lol. Im new ist to c++ and for my uni course we have to construct a deck of card with a build_deck function. I managed to get the function working but when i try to test it ( by calling the function in a main() ) i get the error cannot call member function 'void Deck::build_deck() without object. Here is the function and me trying to test it. Thanks in advance :).

1
2
3
4
5
6
7
8
9
10
11
12
13
void Deck::build_deck()
{

    for( int suit = 1; suit <= 4 && _deal_next != 51; suit++ && _deal_next+13)
        {
            for( int fvalue = 1; fvalue < 13; fvalue++ )
                {
                    _playing_cards[_deal_next] = new PlayingCard( fvalue, suit );
                }
            
        }

}


1
2
3
4
5
6
7
8
9
10
11
12
13
#include "deck.hpp"
#include <iostream>

using namespace std;

int main()
{

    Deck::build_deck();

return 0;

}

Last edited on
Deck::build_deck() is not a static function, so it needs an object first.
1
2
3
4
5
6
int main()
{
    Deck myDeck;
    myDeck.build_deck();
    return 0;
}
hi thanks for the reply! I've changed that and now i get a different error. it says :

in function 'int main()'
no matching function for call to 'Deck::Deck()'

note: candidates are: Deck::Deck(int)
Deck::Deck(const Deck&)
Replace bradw's 3rd line with whatever constructor you wish to use. I see you have on with an int as argument, so just use that one.
Deck myDeck(<someint>);
candidates are:
Deck::Deck(int)
Deck::Deck(const Deck&)


Your Deck constructor requires an argument.

I suggest you read the documentation, header, or constructor code to find out what the value it wants represents. I suspect it would be the number of cards, but it could be the number of Decks, or anything else.

1
2
Deck myDeck(1); // int constructor
Deck anotherDeck(myDeck); // const Deck& constructor 


I really have no idea what the Deck class does, so you should look it over so you know how to use it properly. I can only expect you will continue to run in to problems until you do. Maybe it came with some kind of example/sample usage.

Thanks guys, ill let you know what happens when i can, i have to use the university computers as they are running linux (gnome i think) and it compiles fine on their machines. but my laptop i use dev c++ and that throws up loads of errors when i compile ( even though the code's fine ) and code blocks also gives me errors :/

bradw said:
Maybe it came with some kind of example/sample usage.


It doesn't come with an example as i wrote the full code :/

Thankyou!
Topic archived. No new replies allowed.