returning arrays from functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

class Cards
{
    public:
    int ncardIndex; // 0-51.
    int ncardValue; // assigned 2-14;; note that all cards have this defined for simplicity's sake. should not be access for JQKA.
    string scardValue; // if Jack Queen King Ace;; note that the other chards will not have this defined.
    string scardSuit; // Diamond, Club, Heart, Spade
    bool bcardDrawn; // false if still in deck.
};

Cards* buildDeck()
{
    Cards *cardDeck = NULL;
    cardDeck = new Cards[52];
    for (int i=0; i<52; i++)
    {
    // code for defining the objects here
    }

    return cardDeck;

}



int main()
{
    Cards* deck = buildDeck();
    cout << deck.scardValue[51];


    return 0;
}


When I try to compile this, I get "error: conversion from 'Cards*' to non-scalar type 'Cards' requested". What am I doing wrong?
It must be deck[51].scardValue.
Line 35 should be deck[51].scardValue.
Oh, thanks, it compiles properly now!
Also, where should i put the delete[] cardDeck? If I place it before the return cardDeck; the output becomes gibberish, but if I place it in main(), it tells me that cardDeck hadn't been defined within the scope.
That's because you named it "deck" in main().
So it should be delete[] deck; within main()?
Yes.
Topic archived. No new replies allowed.