| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"sahm" |
| Date: |
10 Dec 2004 01:12:49 PM |
| Object: |
operator overloading |
i need help to use this function, but use an overloaded operator<<
instead....
ostream operator<<(ostream& out, vector<Card>& deck)
{
int i = 0;
for (int s = 1; s <= 4; s++)
{
for (int r = 1; r <= 13; r++, i++)
{
deck[i].setSuit(s);
deck[i].setRank(r);
deck[i].printCard();
cout << endl;
}
system("pause");
cout << endl;
system("pause");
//return out;
}
}
this works when i compiled it, but after the last set of cards are
printed on d screen, my program exits and there's debugger exception
modification on d screen...
.
|
|
| User: "Victor Bazarov" |
|
| Title: Re: operator overloading |
10 Dec 2004 01:25:32 PM |
|
|
sahm wrote:
i need help to use this function, but use an overloaded operator<<
instead....
ostream operator<<(ostream& out, vector<Card>& deck)
{
int i = 0;
for (int s = 1; s <= 4; s++)
{
for (int r = 1; r <= 13; r++, i++)
{
deck[i].setSuit(s);
deck[i].setRank(r);
Why are you _modifying_ the cards while _outputting_ them? This is
a very strange thing to do.
deck[i].printCard();
cout << endl;
}
system("pause");
cout << endl;
system("pause");
//return out;
}
}
this works when i compiled it, but after the last set of cards are
printed on d screen, my program exits and there's debugger exception
modification on d screen...
The code you posted has numerous assumptions in it, which we cannot
verify without seeing more code. Please use the debugger built into
your IDE to see what's going on. You can tell your debugger to stop
if an exception occurs. But please don't ask how to do it, as it's
beyond the scope of this newsgroup.
V
.
|
|
|
| User: "Iguana" |
|
| Title: Re: operator overloading |
10 Dec 2004 02:20:37 PM |
|
|
"sahm" <abigael_b@yahoo.com> wrote in message
news:1102705969.422959.56170@f14g2000cwb.googlegroups.com...
i need help to use this function, but use an overloaded operator<<
instead....
ostream operator<<(ostream& out, vector<Card>& deck)
{
int i = 0;
for (int s = 1; s <= 4; s++)
{
for (int r = 1; r <= 13; r++, i++)
{
deck[i].setSuit(s);
deck[i].setRank(r);
deck[i].printCard();
cout << endl;
}
system("pause");
cout << endl;
system("pause");
//return out;
}
}
this works when i compiled it, but after the last set of cards are
printed on d screen, my program exits and there's debugger exception
modification on d screen...
use 'out' instead of 'cout' for more flexibility (I think this is where the
problem lies too if you output to cout then send more to it while its open
or something??? something to do with cout being volatile (not the C++
operator meaning, but kinda the same, I think)) can't see it in my head at
the moment
what I'd do is put friend 'ostream &operator<<(ostream& out, vector<Card>&
deck)' in the vector and use the data directly (or use the member functions
in 'deck' (below))
and change the print card to the end of the Fn and... (unless printCard is
doing something else, but if it is why is it in the operator<<() fn)
out << "Suit: " << deck[i].suit << ", Card: " << deck[i].rank << endl;
or along those lines with the 'deck' member functions to get the values
alternativly making a seperate ostream& operator<<(ostream& out, Card&card)
and use that to print the card out seperatly and a member function: const
Card& GetCard(int nSuit, int nRank), in deck
Course I've no real idea of if this'll work, and the code might not be
exact, but I've done similar programs this way
.
|
|
|
| User: "Iguana" |
|
| Title: Re: operator overloading |
11 Dec 2004 01:19:03 PM |
|
|
Sorry... was wrong again... return a reference will fix it I think (not
tested)
"Iguana" <hairnet@f2s.com> wrote in message
news:cpd0e1$94l$1@news.freedom2surf.net...
"sahm" <abigael_b@yahoo.com> wrote in message
news:1102705969.422959.56170@f14g2000cwb.googlegroups.com...
i need help to use this function, but use an overloaded operator<<
instead....
ostream operator<<(ostream& out, vector<Card>& deck)
{
int i = 0;
for (int s = 1; s <= 4; s++)
{
for (int r = 1; r <= 13; r++, i++)
{
deck[i].setSuit(s);
deck[i].setRank(r);
deck[i].printCard();
cout << endl;
}
system("pause");
cout << endl;
system("pause");
//return out;
}
}
this works when i compiled it, but after the last set of cards are
printed on d screen, my program exits and there's debugger exception
modification on d screen...
use 'out' instead of 'cout' for more flexibility (I think this is where
the problem lies too if you output to cout then send more to it while its
open or something??? something to do with cout being volatile (not the C++
operator meaning, but kinda the same, I think)) can't see it in my head
at the moment
what I'd do is put friend 'ostream &operator<<(ostream& out, vector<Card>&
deck)' in the vector and use the data directly (or use the member
functions in 'deck' (below))
and change the print card to the end of the Fn and... (unless printCard is
doing something else, but if it is why is it in the operator<<() fn)
out << "Suit: " << deck[i].suit << ", Card: " << deck[i].rank << endl;
or along those lines with the 'deck' member functions to get the values
alternativly making a seperate ostream& operator<<(ostream& out,
Card&card) and use that to print the card out seperatly and a member
function: const Card& GetCard(int nSuit, int nRank), in deck
Course I've no real idea of if this'll work, and the code might not be
exact, but I've done similar programs this way
.
|
|
|
|
|
|

|
Related Articles |
|
|