Newbie Question
-
I want to compile a project that I have created. The project has class objects separated in different files i.e. main.cpp, card.h, card.cpp, deck.h, deck.cpp. How am I able to do this so the deck class can create in instance of the card class i.e. vector x; I know this is really newbie but I got to know.
-
I want to compile a project that I have created. The project has class objects separated in different files i.e. main.cpp, card.h, card.cpp, deck.h, deck.cpp. How am I able to do this so the deck class can create in instance of the card class i.e. vector x; I know this is really newbie but I got to know.
joshp1217 wrote:
How am I able to do this so the deck class can create in instance of the card class
class deck
{
deck();
~deck();card c;
}
Or you can do it in one of the
deck
's members:void deck::foo( void )
{
card c;
}
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
I want to compile a project that I have created. The project has class objects separated in different files i.e. main.cpp, card.h, card.cpp, deck.h, deck.cpp. How am I able to do this so the deck class can create in instance of the card class i.e. vector x; I know this is really newbie but I got to know.
Remember to make sure that all the files that use a type ( e.g card class ) have a #include line that references the file that defines the type. If card is defined in card.h then deck.cpp might look something like this.
//deck.cpp #include "card.h" deck::deck() { card the_cards[52];//An array of 52 cards for the deck }
If the definition of deck itself, in deck.h, depends on the definition of card then it's deck.h that should#include "card.h"
Think of it as, when the compiler sees#include "card.h"
it goes and reads the card.h file and after that it knows about the card class otherwise it's as if it never heard of it. The trick is that every time it moves on to compiling a different .cpp file (compilation unit) it forgets everything it found out before and starts again knowning nothing except C++. Sounds dumb but it really works ;)Nothing is exactly what it seems but everything with seems can be unpicked.
-
Remember to make sure that all the files that use a type ( e.g card class ) have a #include line that references the file that defines the type. If card is defined in card.h then deck.cpp might look something like this.
//deck.cpp #include "card.h" deck::deck() { card the_cards[52];//An array of 52 cards for the deck }
If the definition of deck itself, in deck.h, depends on the definition of card then it's deck.h that should#include "card.h"
Think of it as, when the compiler sees#include "card.h"
it goes and reads the card.h file and after that it knows about the card class otherwise it's as if it never heard of it. The trick is that every time it moves on to compiling a different .cpp file (compilation unit) it forgets everything it found out before and starts again knowning nothing except C++. Sounds dumb but it really works ;)Nothing is exactly what it seems but everything with seems can be unpicked.
I thought that was how it should work but when I sent it up that way, i was getting errors like the Deck class didnt recognize the Card class because when i said vector m_Cards;(this is in the deck.h with #include "card.h" at the top) it basically would say that a vector cant have any empty type. The IDE i am using is Dev-C++ I dont know if that has anything to do with it but any further help would greatly be appreciated because I could do this with ease in C# but I need to do it in C++.
-
I thought that was how it should work but when I sent it up that way, i was getting errors like the Deck class didnt recognize the Card class because when i said vector m_Cards;(this is in the deck.h with #include "card.h" at the top) it basically would say that a vector cant have any empty type. The IDE i am using is Dev-C++ I dont know if that has anything to do with it but any further help would greatly be appreciated because I could do this with ease in C# but I need to do it in C++.
Should that perhaps be
vector<card> m_Cards;
or did the html formatter just make your vector type parameter disappear?:omg: If not it might explain the empty type error, might even need#include<vector>
andstd::vector<card>m_Cards;
There's very little of theusing AClass::BClass;
stuff in C++ althoughusing namepsace std;
works just fine and should let you use a vector as per the first example above. I'm no STL guru but then if you've done loads of C# you're no newbie either :-DNothing is exactly what it seems but everything with seems can be unpicked.