C++ Card Class?

I am supposed to write a Card class for my C++ class and have it display a single card. It builds with no errors, but when I go to run it, it does not display the card. I don't know what I'm doing wrong here.

Although it does give me one warning:

Warning C4717: 'Card::Card' : recursive on all control paths, function will cause runtime stack overflow

card.h

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
#ifndef CARD_H
#define CARD_H
#include <iostream>
#include <string>
using namespace std;

class Card
{
	public:
		Card();
		void setSuit(string x)
		{
			suit = x;
		}
	
		string getSuit()
		{
			return suit;
		}
		void setCard(int y)
		{
			card = y;
		}

		int getCard()
		{
			return card;
		}
	private:
		string suit;
		int card;
};


#endif 


card.cpp

1
2
3
4
5
6
7
8
9
10
11
12
#include "card.h"
#include <iostream>

using namespace std;


Card::Card()
{
	Card ten;
	ten.setCard(10);
	ten.setSuit(" of Spades");
}


main.cpp

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "card.h"

using namespace std;

int main()
{
	Card();
	return 0;
}
 
Card ten;


you are creating a card in the constructor of card. Which in turn will create another card. Which will create another card. ad infinitum.
That states the problem, however it does not help to resolve it.
Yes it does. Just don't do stuff like that. I told you what's wrong, now fix it.
Last edited on
You talk as though I understand what you're saying.
I just told you exactly where and what's wrong. What more do you need? If you don't know enough C++ to understand what people tell you maybe, just maybe, the problem is on your end.
You may easily understand how to fix it, because of that knowledge you hold, you believe everyone else can understand how to do it as well. You've probally been programming alot longer than I have, Me: I only have 3 weeks in programming C++. So forgive me for saying this, But right now you are unknowingly being quite rude and disrespectful.

That is why this specific Thread is the Beginners Section, because I am indeed a Beginner.

For me, being told what & where the problem is in my code does not help me understand how to fix the problem.
Last edited on
I don't know how I can phrase "you create a card in the constructor of card, and this in turn leads to the constructor being called again and again" any simpler. It's neither disrespectful nor rude to expect someone to either know or learn the terminology of the problem when they ask a question.
closed account (DSLq5Di1)
This picture may help illustrate what the problem is and how to solve it:-
http://i54.tinypic.com/2ajbbet.jpg
Topic archived. No new replies allowed.