Reputation: 23
I am currently in the beginning stages of learning C++ (coming from a Java background), and I am having difficulty understand why my program doesn't work. The goal of my program is to be able to create a Card object, in which a random enumerated type (either Infantry, Cavalry or Artillery) will be associated with that object. This means that every time I create a new Card, this Card will have the label of either Infantry, Cavalry or Artillery.
Here is what I have so far:
Card.h
#include <iostream>
#include <string>
using namespace std;
class Card
{
public:
enum CardType
{
INFANTRY,
CAVALRY,
ARTILLERY
};
Card();
Card(CardType type);
CardType GetType();
string toString(CardType type);
private:
CardType type;
};
Card.cpp
#include "Card.h"
#include <iostream>
using namespace std;
Card::Card() { }
Card::Card(Card::CardType type) // creates a card with a random CardType
{
type = static_cast<Card::CardType>(rand() % 3);
}
Card::CardType Card::GetType()
{
return type;
}
string Card::toString(Card::CardType type)
{
switch (type)
{
case Card::CardType::INFANTRY: return "Infantry";
case Card::CardType::CAVALRY: return "Cavalry";
case Card::CardType::ARTILLERY: return "Artillery";
}
}
Driver.cpp
#include <iostream>
#include "Card.h"
using namespace std;
int main()
{
Card c;
c.toString(c.GetType());
}
Visual Studio is able to build my program, but freezes right after building it so I'm guessing that I did something wrong.
Would there be a more simpler/efficient way to achieve what I want my program to do?
I don't really understand much of C++ yet, so any pointers will help.
Thank you for your input!
Upvotes: 2
Views: 129
Reputation: 322
The problem here is that you create a Card c
which, if we look at the constructor Card(), doesn't initialize the attribute type.
So in your case type has any value you could imagine but certainly not in the range of your CardType enumeration, since there is no default value in C++ like there is in Java.
Then you call toString which do switch(type). You don't have default case and type isn't any of the case values so the function actually never returns !
You can fix that easily like that :
string Card::toString(Card::CardType type)
{
switch (type)
{
case Card::CardType::INFANTRY: return "Infantry";
case Card::CardType::CAVALRY: return "Cavalry";
case Card::CardType::ARTILLERY: return "Artillery";
default: return "ERROR";
}
}
In any case on the long run your constructors should look like that :
Card::Card() { type = static_cast<Card::CardType>(rand() % 3); }
Card::Card(Card::CardType type) // creates a card with a random CardType
{
this->type = type;
}
And on an other thing that could be part of the problem. There is something called Include guard in C++ you can look a bit more here : https://en.wikipedia.org/wiki/Include_guard
Cheers.
Upvotes: 0
Reputation: 1074
The problem is with your object creation.
Because, there are two constructors in your card class i.e..Default and parametric constructors, and when you create an object using the Card c;
statement, the default constructor is invoked. So, in order to invoke the parametric constructor, you have to use Card c(Card::CardType::INFANTRY);
.
Upvotes: 1