Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
02-14-2006, 11:16 PM
|
#1
|
Senior Member
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539
Rep:
|
composition, and template-based Index
k im quite stumped... ill try and be straightforward with my question.
1
i have a header file i made, named Card, to model a playing card. the constructor takes in (char, str).
2
i have a class called SecondaryIndex.. which is a template-based Index-type of list, so it can store any data type.
3
i have another class called Deck (to model a deck of cards).. heres the layout of it so far:
Code:
class Deck
{
private:
SecondaryIndex<Card>* index;
public:
Deck();
~Deck();
};
Deck::Deck()
{
index = new SecondaryIndex<Card>;
}
yes i have other stuff above (includes, etc) and below (function implementations, etc)
the thing is.. the line where i declare my index variable, originally i didnt have it a pointer.. but i think i have to make it a pointer because it REQUIRES parameters, and u cant instantiate your objects outside of functions, correct? (without using static or something like that)
so i figured i can make it a pointer.. then just initialize it in the constructor. this isnt working out for me though. both the card and secondaryIndex classes REQUIRE at least 1 parameter.. so where do i put these parameters or when? or is my logic totally wrong?
when i do: (in constructor)
index = new SecondaryIndex;
i get this error:
`SecondaryIndex' is not a type
when i do: (in constructor)
index = new SecondaryIndex<Card>;
i get this error:
no matching function for call to `SecondaryIndex<Card>::SecondaryIndex()'
im not sure what to put
any tips appreciated.. thanks
Last edited by nadroj; 02-14-2006 at 11:20 PM.
|
|
|
02-15-2006, 09:06 AM
|
#2
|
Member
Registered: Oct 2005
Posts: 970
Rep: 
|
Can I see the full code please. I wouldn't like to comment until I have seen it.
cheers
Last edited by dmail; 02-15-2006 at 09:08 AM.
|
|
|
02-15-2006, 09:24 AM
|
#3
|
Senior Member
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539
Original Poster
Rep:
|
sure.. but that basically was the full code.
Code:
#ifndef JI_DECK
#define JI_DECK
#include "SecondaryIndex.h"
#include "Stack.h"
#include "Card.h"
using namespace std;
class Deck
{
private:
SecondaryIndex<Card>* index;
public:
Deck();
~Deck();
};
Deck::Deck()
{
index = new SecondaryIndex<Card>();
// this is the line iv been playing around with.. no clue
what to put to create the new SecondaryIndex which stores
types of 'Card's'
}
Deck::~Deck()
{
if (index != NULL)
delete index;
}
#endif
Last edited by nadroj; 02-15-2006 at 09:26 AM.
|
|
|
02-15-2006, 09:38 AM
|
#4
|
Member
Registered: Oct 2005
Posts: 970
Rep: 
|
I meant SecondaryIndex.h and Card.h 
this error:
no matching function for call to `SecondaryIndex<Card>::SecondaryIndex()'
implies to me theres no default constructor, but then again I may be wrong.
Last edited by dmail; 02-15-2006 at 09:47 AM.
|
|
|
02-15-2006, 03:33 PM
|
#5
|
Senior Member
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539
Original Poster
Rep:
|
::EDIT:: sorry this post got really long. ill try to sum up my [new] question in afew lines:
- how can u DECLARE (ie not 'instantiate') a variable of some type, whos type _requires_ at least one parameter? and later on you can create this object. ill give example in java code:
Code:
String joe; // creates variable. nothing in it though.
joe = new String("text"); // now we have an object.
because in c++, if i hade 'SecondaryIndex<Card> index' this CREATES the object correct?
Done EDIT::
oh, sorry.
i changed my class so that there is a constructor that doesnt take in parameters... so i dont have to make the SecondaryIndex a pointer or anything... because i can now just do 'SecondaryIndex<Card> index();' in the private section of the class.
however.. i would like to know how you can do this? i mean.. (im used to java and having to use 'new' to instantiate objects), say i had a variable, say Card... and i wanted it as a private member. lets say Card had only 1 contructor, and it took in parameters. however, at run time i didnt know the parameters or something... how can i define this?
heres what im trying to say (hopefully will clear it up and make sense)
Code:
class SomeClass
{
private:
Card myCard; // this CREATES a card object correct?
//However, 'Card' MUST take in parameters to the constructor..
//so it should be something like: Card myCard(x,y); or somethign.
//however.. i do not know or want to set these values right now.
public:
myClass()
{//in the constructor is when i want to instantiate the 'Card'
//..this is how i am thinking about it (in java code)
myCard = new Card(3,5); //(or whatever the param's are)
}
};
however.. i got this part to work in my code.. i mean i changed the SecondaryIndex so it doesnt take in parameters.. this is what i have (cut down for simplicity sake):
Code:
using namespace std;
class Deck
{
private:
SecondaryIndex<Card> index();
Stack<Card> stack();
public:
Deck();
~Deck();
void Initialize();
};
ok.. besides this stuff (since i have it working, but still dont know why i couldnt do it how i was doing it).. im on to another problem.
the 'initialize()' function is used to fill a 'SecondaryIndex' with the 52 cards in a deck... and i think the next question is similar to the problem mentioned above (about instantiation objects at certain times, or something, lol.. i dunno)
Code:
void Deck::Initialize()
{
srand( time(NULL));
for (int suit = 3; suit < 6; suit++) //3= hearts.. 4=spades.. etc
{
for (int face = 1; face < 14; face++) //ace throug h king
{
Card* newCard;
//i made newCard a pointer so i dont have to initialize this varible.
//because if i initialize it now, with 2 random parameters,
//how can i 'recreate' (?) this variable, in the below if statements? (ie newCard = NULL; newCard = Card(x,y); or somtehing?
if (face == 1) // == ace
newCard = &(Card(suit,"A"));
else if (face == 11) // == jack.. etc
newCard = &Card(suit,"J");
else if (face == 12)
newCard = &Card(suit,"Q");
else if (face == 13)
newCard = &Card(suit,"K");
else
{
string temp = "" + face;
newCard = &Card(suit, temp );
}
index.add(*newCard);
}
}
}
the 'face' variable is the ace, jack, or 4, or 9.. etc.. 'suit' is of course the integer that represents hearts, etc.
ill explain my motive for doing what i have shown. i have a loop to go through the 4 suits in a deck. inside that is a loop to go through the 14 cards in each suit. theyre are special cases for ace or king of course, which is the purpose of the if statement.
at the end of the if block, i have a line of code that will happen no matter what.. however, the local variable created in each if block (newCard) would have been distroyed by then. so i need to make it have a larger scope. which is why i declare it before the if block.
one solution to do this is the following:
Code:
if (face == 1) // == ace
{
Card newCard(suit,"A");
index.add(newCard);
}
else if (face == 11) // == jack.. etc
Card newCard(suit,"J");
index.add(newCard);
}
else if (face == 12)
Card newCard(suit,"Q");
index.add(newCard);
}
else if (face == 13)
Card newCard(suit,"K");
index.add(newCard);
}
else
{
string temp = "" + face;
Card newCard(suit, temp ); //2nd parameter must be string.. thats why i do the previous line
}
}
however.. its alot of repetition (kinda).. but thats not the point. the point is that i SHOULDNT have to repeat the 1 command. i hope this makes sense to what im asking.
sorry for the long and somewhat off-topic post.. and thanks for any input
Last edited by nadroj; 02-15-2006 at 03:38 PM.
|
|
|
02-15-2006, 04:58 PM
|
#6
|
Senior Member
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539
Original Poster
Rep:
|
k i found a way around it.. but still not satisfied, but ill leave it how i am doing it now. here it is:
Code:
void Deck::Initialize()
{
for (int suit = 3 ; suit < 7; suit++)
{
for (int face = 1; face < 14; face++)
{
string strFace;
if (face == 1)
strFace = "A";
else if (face == 11)
strFace = "J";
else if (face == 12)
strFace = "Q";
else if (face == 13)
strFace = "K";
else
{
stringstream s;
s << face; // convert the int to a 'string', as required in 2nd parameter of Card(char,string)
s >> strFace;
}
Card newCard(suit, strFace );
index.add(newCard);
}cout << endl;
}
}
it works exactly how i wanted it to.
|
|
|
02-15-2006, 05:24 PM
|
#7
|
Member
Registered: Oct 2005
Posts: 970
Rep: 
|
Wooooo think you lost me there, but I'm glad you got it working.
Just a couple of Q's why does suit start @ 3?
and whats wrong with the old fashioned method
Code:
enum SUIT{SPADES=0;DIAMONDS;HEARTS;CLUBS};
enum VALUE{TWO=2;THREE;FOUR;FIVE......};
//and then
Card newCard( SUIT, VALUE );
index.add(newCard);
Last edited by dmail; 02-15-2006 at 05:27 PM.
|
|
|
02-15-2006, 05:44 PM
|
#8
|
Senior Member
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539
Original Poster
Rep:
|
First, thanks for hanging around. Second, sorry for the width/format of this post.
Card's constructor takes in (char, string), where char is suit and string is the face, ie "A" or "10", etc. i start at 3 because setting a char to 3 displays the Heart suit.
lol  i was bound to get lost in my own confusion.
sure.. i have it working now, but i did it a different way. and i like to know why i couldnt (or how i can) do it the previous way.
im newer to c++ but id say im intermediate in java. hopefully you know java as well (i would guess you do, at least the basics if nothing else). ill try and describe what i was trying to do in java.
lets pretend my Card.h was written in java (Card.java). it still has the same insides (same constructor (char, string), methods, etc), but just written in java.
now, i can declare a variable of type 'Card' by doing:
Card myCard;
However.. the value right now of myCard would be NULL (or some random address). i must 'instantiate' it first. so i would type:
myCard = new Card(4,"Ace");
or something similar. this now creates my object of type Card.
hopefully that makes sense; this is what i was trying to do in C++. (create a variable that doesnt have anything in it, then instantiate it later). of course, you know, that 'myCard = new Card(4,"Ace");' wouldnt work the same in c++. 'new' is used to allocate dynamic memory.
so how, in c++, can i create a variable of type 'Card'.. which doesnt call the constructor with the declaration, or store anything in it. then, i would like to later call its constructor and actually create the object.
i figured i could do it with a pointer, ie:
Card* pMyCard; // just a place to store it.. right?
then, later on in the code (ie in the big 'if' block in previous post) i could use this variable, since it's scope is still there. and do something like:
pMyCard = &Card(4,"Ace");
this would [i thought] create a 'Card' object with the parameters (4,"Ace"), get the address of this new object, then assign pMyCard pointer to point to it's address. this made logical sense and i was happy when i thought of it.. but it didnt seem to work, and it wouldnt compile
i sure hope that makes it a little more clear about what i was/am trying to do... thanks for any input!
Last edited by nadroj; 02-15-2006 at 05:53 PM.
|
|
|
02-15-2006, 06:04 PM
|
#9
|
Member
Registered: Oct 2005
Posts: 970
Rep: 
|
Quote:
so how, in c++, can i create a variable of type 'Card'.. which doesnt call the constructor with the declaration, or store anything in it. then, i would like to later call its constructor and actually create the object.
i figured i could do it with a pointer, ie:
Card* pMyCard; // just a place to store it.. right?
|
This is correct yes.
Quote:
then, later on in the code (ie in the big 'if' block in previous post) i could use this variable, since it's scope is still there. and do something like:
pMyCard = &Card(4,"Ace");
this would [i thought] create a 'Card' object with the parameters (4,"Ace"), get the address of this new object, then assign pMyCard pointer to point to it's address. this made logical sense and i was happy when i thought of it.. but it didnt seem to work, and it wouldnt compile
|
scope is the key word here. you want to store the address of a card but you pass it the address of a card whos scope is local and ends; therefore what your pointer points to is not safe. As for not wanting to compile I wonder if there is another problem with
Code:
pMyCard = &Card(4,"Ace");
Ive never tried it for the above reason.
I would just use
Code:
pMyCard = new Card(4,"Ace");
Last edited by dmail; 02-15-2006 at 06:08 PM.
|
|
|
02-15-2006, 06:23 PM
|
#10
|
Senior Member
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539
Original Poster
Rep:
|
Thanks.
ok it seems we're on the same track more or less.
ok so i make a pointer with new and use delete to free it up when done.
do u get what im saying now? i mean, is the only way to do something like this to use a pointer? doesnt seem to make sense to me. i dont like how in c++ the constructor is automatically called when u do somethign like 'Card myCard;'
|
|
|
All times are GMT -5. The time now is 10:41 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|