printing from a vector

Hi guys, I have recently created a card class like this:

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;
            }
};


the problem is, that i have a vector of type 'Card' and need to print out the full vector, to create the vector i have this:

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
void createDeck()
{
    int n,i,j;
    string b;
    j = 0;

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

                case 2: b = "Spades";
                break;

                case 3: b = "Diamonds";
                break;

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

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

        }
    }

}


how do i go about accessing the value and suit from 'deck1[x]' (for loop prefreble). when i place a dot (.) afterwards, it gives me vector options, not my class options.
afterwards, it gives me vector options, not my class options.

Sounds like a bug in your editor's code completion.
It's deck1[x].getvalue().
ok i have that sorted, there is a new error on my card class saying it is being 'redefined'
Well, sounds like you did something wrong (such as trying to redefine the Card class).
Last edited on
How is that acheived?
My crystal ball is broken, but if I had to guess, then I'd say you're including the card header twice and forgot the include guard.
so, the #include "class.cpp"?
No, you don't include .cpp files.
Seriously, post your code.
ok, here it is

firstly my main file:

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
#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 = 1; n < 5; n++)
    {
        for(i = 1; i < 14; ++i)
        {
            switch (n)
            {
                case 1: b = "Hearts";
                break;

                case 2: b = "Spades";
                break;

                case 3: b = "Diamonds";
                break;

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

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

        }
    }

}


int main()
{
    return 0;
}


my 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
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include "class.cpp"

using namespace std;

class Deck
{

    private: vector<Card> cardvector;

    public:

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



            void printdeck ()
            {
                for (int x; cardvector.size(); ++x)
                {
                    cout << cardvector[x].getvalue() << " of ";
                    cout << cardvector[x].getsuit();
                }
            }

            void nextcard ()
            {

            }

            void shufflecards ()
            {

            }
};


and lastly my 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;
            }
};
Yeah, you're including "class.cpp" twice in main.cpp. First directly, then indirectly via "deck class.cpp".
Didn't your book teach you about translation units and header/source files?
aha! it builds now, thank you, i have no book, just a lecturer who talks cryptic and barely explains stuff
#include "class.cpp"
#include "deck class.cpp"

I would refactor the names of those files to be .h so that when you include them they look like this in the code.

1
2
#include "class.h"
#include "deck class.h" 
Topic archived. No new replies allowed.