operator overloading

S

sahm

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.setSuit(s);
deck.setRank(r);
deck.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...
 
V

Victor Bazarov

sahm said:
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.setSuit(s);
deck.setRank(r);


Why are you _modifying_ the cards while _outputting_ them? This is
a very strange thing to do.
deck.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
 
I

Iguana

sahm said:
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.setSuit(s);
deck.setRank(r);
deck.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.suit << ", Card: " << deck.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
 
I

Iguana

Sorry... was wrong again... return a reference will fix it I think (not
tested)

Iguana said:
sahm said:
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.setSuit(s);
deck.setRank(r);
deck.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.suit << ", Card: " << deck.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,578
Latest member
LC_06

Latest Threads

Top