|
Hi,
I've been handling with a soccer game using STL and have the following function..
set<game>::iterator gameIterator = tournament->sche->games.begin();
Game *currentGame = new Game();
*currentGame = *gameIterator;
Then i have a function:
startGame(currentGame);
however, after the fuction, the currentGame's properties are updated. but not the properties for that particular game in
tournament->sche->games
Can anyone tell me why?
Thanks..
Jass
|
|
|
|
|
*currentGame = *gameIterator; This makes a copy of the object pointed to by gameIterator . Any changes you then make to currentGame are not reflected in your set tournament->sche->games .
I would suggest
set<Game>::iterator currentGame =
tournament->sche->games.begin();
startGame( currentGame ); and change the declaration and implementation of startGame to take a set<Game>::iterator instead. You'll then be working directly with your container.
You might need to check the definition of set<> 's member functions to check that it won't produce copies.
By the way, is there some reason you need an ordered unique container of Game objects? I would normally recommend using a list<> , vector<> or deque<> for an arbitrary collection.
Use list if you need to insert or delete from the middle of the list, and you don't need to access elements by index very often. Use vector if you need to be compatible with C arrays, index a lot, don't add or delete in the middle of the container or at the beginning, and rarely add new elements to the end. Use deque if you need to access by index but still need to add or delete in the middle of the container.
|
|
|
|
|
set::iterator gameIterator = tournament->sche->games.begin();
1. gameIterator now points to the tournament->sche->games.begin()
Game *currentGame = new Game();
2. currentGame now points to a new Game
*currentGame = *gameIterator
3. You new Game now has same data as the tournament->sche->games.begin(); because of member-wise assignment.
startGame(currentGame);
4. You passed in pinter to new Game made in step 2. you DID NOT pass in any reference to the original tournament->sche->games.begin()
Result : you modified the contents of the new Game filed and nothing to do with the contents or values of the iterator.
A much simpler example:
int * px = new int;
int * py = new int;
*py = *px <- y now has value of x
SomeFunction(py);
is only going to modify the value of y, x will remain entirely unaffected.
The memories of a man in his old age are the deeds of a man in his prime.
|
|
|
|
|
Hi all...
How can I Receive Remote machine Information????
|
|
|
|
|
Well that would depend what kind of information you are looking for now wouldn't it? The canonical answer is probably WMI.
--
-Blake (com/bcdev/blake)
|
|
|
|
|
Dear all,
I am developing an application using MFC VC++ Version 6.0 for Windows 2000.
I need to capture login and log off event in my application. Is there any event (or API) to capture these events
Thanks
rohit
|
|
|
|
|
If your application is an ordinary application, it will be terminated when the user logs off, because Windows tears down the session.
Windows sends a WM_QUERYENDSESSION message, followed by a WM_ENDSESSION message, when the user logs off or shuts down the system. See the documentation for the exact semantics.
If you're trying to handle user switches on Windows XP, see the KB article HOW TO: Write an Application That Supports Fast User Switching in Windows XP[^].
(edited for formatting)
|
|
|
|
|
Thanks mike you solved my purpose
|
|
|
|
|
Can somebody help me with getting my pointer declared correctly so I can access the unsigned int x in 2 byte pieces? The book I have is very vague on how I am supposed to go about. Not much for example code. But I think I am close.
Here ya go:
<pre>typedef struct
{
unsigned short int x;
} myStruct;
void main()
{
char c;
myStruct s;
s.x = 1;
((char *) &s.x)[0]; //subscript to read first byte
printf("%d\n",&s.x);
} </pre>
|
|
|
|
|
I think your question was answered elsewhere, but it might be helpful to know about union structures to accomplish something like this.
Example:
union PeekInt {
unsigned short int i;
struct {
char b1;
char b2;
} bytes;
} mpi; This specifies that the mpi.bytes structure occupies the same memory space as mpi.i , so mpi.bytes.b1 will let you access the first byte and mpi.bytes.b2 will let you access the second byte.
- Mike
|
|
|
|
|
Yes, thank you for that example. Thats what my book(Daniel W. Lewis, "Fundamentals of Embedded Software") needs to focus on, putting more examples in it. It don't really show you much examples or how to's, it just assumes everything is bread and butter to you. Very frustrating. With your example and a few other tips I got from this forum, I am going to be ready for the test tomorrow.
Thank you.
Brian.
|
|
|
|
|
Hi I have MS Visual C++ 1.32, is there any possible way I can upgrade to something newer without paying the cost of 6.0 itself. I mean, is there a free way to upgrade to 4.0 or 5.0 or even 6 without buying the full 6.0 version.
I may sound cheap, but I'm in a college class for C++ and It would really help me if I had a newer version. 1.32 doesn't work with all the code that 6.0 does.
Well if there is anyway to upgrade I would really appreaciate the help!
Sincerely
Matt
|
|
|
|
|
It's amazing that you have version 1.32! The oldest version I have is 1.52c. You can pick up version 6.0 Professional for about $50 at this EBay auction[^] if you hurry. Good luck!
/ravi
Let's put "civil" back in "civilization"
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
For a definitive answer, check out this Web site! It looks like the oldest version they'll let you upgrade from is 5.
These may also be of interest to you:
http://www.msdnaa.net/factspage.aspx
http://www.microsoft.com/education/?ID=HowToBuy
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
|
Can somebody tell me what I am doing wrong what the correct syntax is for formatting your output in binary representation.
I have been using printf("%b\n",variable) like the the help files says in .NET but it just keeps outputing a b on the console.
And does anyone know the correct syntax in C for packing your structures?
Help.
Thanks.
|
|
|
|
|
I think you have been looking at the wrong printf format specification.
There is no format that outputs binary in C (neither std or ms extension).
There is no specific packing or alignment keywords in C.
Use the pack pragma and the align declspec.
|
|
|
|
|
Okay, I found something about that pragma but Im not sure how it works with packing the data. Hmmm. Got any ideas.
I found: pragma pack(push, 1) or something like that. Not sure what it does though.
Does C++ have binary formatting?
|
|
|
|
|
About formating, nope. Actually it is printf that lacks it and it's the same for both languages.
Concerning packing, what are you trying to accomplish? Kind of hard to try to help without more info...
|
|
|
|
|
What I'm trying to do is write a struct in C with a couple data types in it, i.e. int, char.
<pre>typedef struct
{
int x;
char c;
}_attribute_((packed))myStruct</pre>
This syntax is for djgpp compiler, I was wondering what you use for .NET or Visual C 6.
|
|
|
|
|
#pragma pack(push, 1)
What this does is set the struct packing to 1-byte boundaries (i.e., no padding), while saving the previous packing. Specify this before your struct, then use #pragma pack(pop) after the struct to restore the old packing. Example:
<br />
#pragma pack(push,1)<br />
struct MyStruct {<br />
int foo;<br />
char a;<br />
};<br />
#pragma pack(pop)<br />
- Mike
|
|
|
|
|
Thanks for the help, hopefully with I can get this thing working.
|
|
|
|
|
Sirrius wrote:
Can somebody tell me what I am doing wrong what the correct syntax is for formatting your output in binary representation.
I have been using printf("%b\n",variable) like the the help files says in .NET but it just keeps outputing a b on the console.
You'll need to write a xxxToBinary() conversion routine. Then you can use printf("%s\n", ...) to output the result.
Sirrius wrote:
And does anyone know the correct syntax in C for packing your structures?
By default, structures are aligned on 8-byte boundaries. To align on some other boundary, such as 4 bytes, use:
#pragma pack(4)
before the structure/union is declared.
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
Thanks for the advice on the packing. Im not to clear on the xxxToBinary() conversion routine you talked about. How do I go about doing that?
Thanks for your help.
|
|
|
|
|
Here's a sample:
void DWORD_To_BinaryString(DWORD value,char string[])
{
for (int bit = 0; bit < 32; bit++) {
DWORD mask = 0x80000000 >> bit;
if ((value & mask) == mask) string[bit] = '1';
else string[bit] = '0';
}
string[32] = '\0';
} Note that this function converts any 32-bit value to a 32 character string or 1's and 0's. The destination array must be at least 33 characters long.
Software Zen: delete this;
|
|
|
|