printing a vector of pointers

Pages: 12
in my class, i wish to print a vector of objects via pointers, so that i can add certain functions later, anyway here is my class now:

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
class Deck
{

    private: vector<Card> cardvector; vector<Card *> pcardvector;

    public:

            Deck (vector<Card> cardvector2)
            {
                cardvector = cardvector2;

                for (int x = 0; cardvector.size(); ++x)
                {
                    pcardvector[x] = &cardvector[x];
                }
            }

            void printdeck ()
            {
                for (int x; x < 52; ++x)
                {
                    switch(cardvector[x].getvalue())
                    {
                        case 1: cout << "Ace"; break;

                        case 11: cout << "Jack"; break;

                        case 12: cout << "Queen"; break;

                        case 13: cout << "King"; break;

                        default: cout << cardvector[x].getvalue(); break;
                    }
                    cout << " of ";
                    cout << cardvector[x].getsuit() << endl;
                }
            }


as you can see the printdeck function isn't using pointers yet, i need it to print using the pointers created in the constructor, just in case, here is the class of the card:

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
class Card
{
    private: string suit; int value;

    public:
            Card()//default constructor (no arguments) allows you to make an
            {     //array
                value = 0;
            }

            Card(int v, string s)// allows the private attributes to be set
            {
                value = v;
                suit = s;
            }

            int getvalue()// returns the cards value
            {
                return value;
            }

            string getsuit()// returns the cards suit
            {
                return suit;
            }
};


and this is the my main (includes the function that creates the deck):

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
41
42
43

vector<Card> deck1;

void createDeck()
{
    int n,i,j;
    string b;
    j = 0;

    for(n = 0; n < 4; n++)
    {
        for(i = 1; i < 14; ++i)
        {
            switch (n)
            {
                case 0: b = "Hearts";
                break;

                case 1: b = "Spades";
                break;

                case 2: b = "Diamonds";
                break;

                case 3: b = "Clubs";
                break;
            };

            deck1.push_back(Card(i, b));

        }
    }

}


int main()
{
    createDeck();
    Deck(deck1).printdeck();
    return 0;
}
Just do this?
 
pcardvector[x]->getValue()  //Inside your print function 

in replace of.. in all your code
 
cardvector[x].getvalue()


Is this what you are asking?
Last edited on
yes, i thought it was that as well, however it makes my program crash
but why do you want to use pointer in the vector ?
Here is one problem.
1
2
3
4
5
//where is the stop condition x < cardvector.size()
for (int x = 0; cardvector.size(); ++x)
{
     pcardvector[x] = &cardvector[x]; //and this is a problem see below.
}


pcardvector contains no elements, its size is 0, trying to index into the vector is going to fail. You will need to push_back(&cardvector[x])

1
2
//x is not initialized here
for (int x; x < 52; ++x)


Once you make these changes call the pointer vector like you think it should be called.

1
2
3
4
5
6
7
8
9
10
11
Deck (vector<Card> cardvector2)
            {
                cardvector = cardvector2;

                for (int x = 0; x < cardvector.size(); ++x)
                {
                    pcardvector[x].push_back(&cardvector[x]);
                }
            }


something like that?
Like this.
pcardvector.push_back(&cardvector[x]);
oh, i understand that now, the same way i have done in the createdeck function, yes?
cool .. clanmjc .. good thing to know ..thanks

but how do i display the value which is in
pcardvector

i mean to say how do i display the value of cardvector which is stored in pcardvector;

thanks
xxx
that would also be useful to know, thanks for asking blue
After making the suggested changes above, replace your calls to the cardvector outputs with pcardvector[x]->getSuite().

it has warnings where the for loop arguement is, saying : trying to compare signed and unsigned variables, and the program doesnt actually output anything
Which line?
19 and 27

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
41
42
43
44
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>

using namespace std;

class Deck
{

    private: vector<Card> cardvector; vector<Card *> pcardvector;

    public:

            Deck (vector<Card> cardvector2)
            {
                cardvector = cardvector2;

                for (int x = 0; x < cardvector.size(); ++x)
                {
                    pcardvector.push_back(&cardvector[x]);
                }
            }

            void printdeck ()
            {
                for (int x; x < pcardvector.size(); ++x)
                {
                    switch(pcardvector[x]->getvalue())
                    {
                        case 1: cout << "Ace"; break;

                        case 11: cout << "Jack"; break;

                        case 12: cout << "Queen"; break;

                        case 13: cout << "King"; break;

                        default: cout << pcardvector[x]->getvalue(); break;
                    }
                    cout << " of ";
                    cout << pcardvector[x]->getsuit() << endl;
                }
            }
Thats because vector.size returns size_t, you can change your indices to size_t instead of int and that warning will go away.
what is size_t? i have never heard of that before
ok that compiles with no warning, but it still displays nothing upon running, just returns zero
Post all your modified code.
you are the boss:

main:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <list>
#include "class.cpp"
#include "deck class.cpp"

using namespace std;

vector<Card> deck1;

void createDeck()
{
    int n,i,j;
    string b;
    j = 0;

    for(n = 0; n < 4; n++)
    {
        for(i = 1; i < 14; ++i)
        {
            switch (n)
            {
                case 0: b = "Hearts";
                break;

                case 1: b = "Spades";
                break;

                case 2: b = "Diamonds";
                break;

                case 3: b = "Clubs";
                break;
            };

            deck1.push_back(Card(i, b));

        }
    }

}


int main()
{
    createDeck();
    Deck(deck1).printdeck();
    return 0;
}


deck class:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>

using namespace std;

class Deck
{

    private: vector<Card> cardvector; vector<Card *> pcardvector;

    public:

            Deck (vector<Card> cardvector2)
            {
                cardvector = cardvector2;

                for (size_t x = 0; x < cardvector.size(); ++x)
                {
                    pcardvector.push_back(&cardvector[x]);
                }
            }

            void printdeck ()
            {
                for (size_t x; x < pcardvector.size(); ++x)
                {
                    switch(pcardvector[x]->getvalue())
                    {
                        case 1: cout << "Ace"; break;

                        case 11: cout << "Jack"; break;

                        case 12: cout << "Queen"; break;

                        case 13: cout << "King"; break;

                        default: cout << pcardvector[x]->getvalue(); break;
                    }
                    cout << " of ";
                    cout << pcardvector[x]->getsuit() << endl;
                }
            }

            void nextcard ()
            {

            }

            void shufflecards ()
            {

            }
};



card class:

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
#include <iostream>
#include <string>

 using namespace std;

 class Card
{
    private: string suit; int value;

    public:
            Card()//default constructor (no arguments) allows you to make an
            {     //array
                value = 0;
            }

            Card(int v, string s)// allows the private attributes to be set
            {
                value = v;
                suit = s;
            }

            int getvalue()// returns the cards value
            {
                return value;
            }

            string getsuit()// returns the cards suit
            {
                return suit;
            }
};
Do you see the problem here?
for (size_t x; x < pcardvector.size(); ++x)
Pages: 12