I'm a little new to this, So I might accidentally call something by the wrong name, so bare with me for a moment.
I am making a Black Jack game, so far I have 3 class's, the deck, the dealer, and the player. I create the deck object in main (deck1), but if I make a statement in a dealer function such as
Deck1.GetCard();
I get a compiler error. It says Deck1 was not declared in this scope. My work around is returning control back to main, getting a card (as an integer) then feeding that integer as a dealer function parameter.
My question: How can I have a class use a function from a different class?
#include <iostream>
usingnamespace std;
class NeedsADog
{
public:
void GetRandomDog(); // Attempts to use a Dog function
};
class Dog
{
public:
void ThingsDogsDo();
};
void NeedsADog::GetRandomDog()
{
WoofieMcWoofienstein.ThingsDogsDo(); // Uses a specific object declared in main
} // Error: WoofieMcWoofienstein was not declared in this scope
void Dog::ThingsDogsDo()
{
cout << "Woof";
}
int main()
{
NeedsADog LittleBoy; //cuz every little boy should have a dog
Dog WoofieMcWoofienstein; // Great name for a dog, if I do say so myself
LittleBoy.GetRandomDog; // The little boy is trying to get a dog, any dog will do
return 0; // Error: statement cannot resolve address of overloaded function
}
Compiling that chunk of code gives 2 errors, I commented on the line that the errors point to, and gave the error it stated. They are found on lines 19 and 31
Thank you, so does that mean I would have to make the deck a subclass of the dealer? I was hoping to make the deck its own class, so I could just copy paste it easier to other card game related projects.
I'm terrible with headers, couldn't get it to work through headers (most likely just because I'm so terrible at them)
I figured it out, passing through by reference and rearranging the way I declared my prototypes. Also, the line 31 error was because I forgot to include the (). Anyways, for anyone who also comes across this problem, the updated code with comments on what I changed to get it to work.
#include <iostream>
usingnamespace std;
class Dog
{
public:
void ThingsDogsDo();
};
class NeedsADog
{
public:
void GetRandomDog(Dog &rDog); // Attempts to use a Dog function, recieves a reference to the dog object passed through from line 32
}; // The dog class needs to be declared first, so that GetRandomDog can have a Dog object defined as a parameter
void NeedsADog::GetRandomDog(Dog &rDog)
{
rDog.ThingsDogsDo(); // Uses a specific object declared in main
}
void Dog::ThingsDogsDo()
{
cout << "Woof";
}
int main()
{
NeedsADog LittleBoy; //cuz every little boy should have a dog
Dog WoofieMcWoofienstein; // Great name for a dog, if I do say so myself
LittleBoy.GetRandomDog(WoofieMcWoofienstein); // I pass through the object to NeedsADog on line 13
return 0;
}
Oh, and even though it's not directly related to the topic: A 'subclass' is a class which inherits from another class. It's part of object oriented design.
To give an example:
Cars and Planes are different. If you wanted to create car objects, you would write a car class, if you wanted to create plane objects, you would create a plane class. However, both car and plane have similar properties, like a maximum speed, the ability to move etc. Here you could write a superclass "Vehicle" that contains all properties and methods all vehicles have in common. Car and Plane would be subclasses of that class. (So basically, subclass and superclass are relative words: You normally don't say "this class is a superclass", but rather "this class is the superclass of that class" or "this class is a subclass of that class".