|
Oh dear, I am so silly :P
That ought to teach me not to reuse old code that has never been tested.
It was simply a pointer to a static int (that was supposed to be) declared in the tile class. Stupid me, put it outside of the tile class for some unknown reason, which made it inaccessible to the Tile class.
Unfortunately though, removing it doesn't fix the problem
I will try what you said and get back to you, though for the mean time I have another question: How do you use a static variable to count the number of classes you have? I can do everything but initialise the variable - C++ simply doesn't let you initialise non const member variables within a class, so where am i supposed to initialise the count to 0? Take the following code as an example;
class theClass
{
public:
theClass(void);
~theClass(void);
private:
static int theCount = 0;
};
theClass::theClass()
{
theCount++
}
theClass::~theClass()
{
theCount--
}
|
|
|
|
|
Header:
class CMyClass
{
public:
CMyClass ();
~CMyClass ();
protected:
static int g_MyCounter;
};
cpp file:
int CMyClass::g_MyCounter = 0;
CMyClass::CMyClass ()
{
g_MyCounter++;
}
CMyClass::~CMyClass ();
{
ASSERT(g_MyCounter);
g_MyCounter--;
}
Given that you have a fixed number per block, I really can't see any point in you doing this for this application though... That static member variable is shared between all instances of CMyClass. I use this technique a lot for registered messages.
eg:
UINT CMyTopSecretClass::m_wm_SomeMessage = ::RegisterWindowMessage ("CMyTopSecretClass::m_wm_SomeMessage");
I hope this helps,
Iain.
|
|
|
|
|
Thanks, I thought that's how you were meant to do it, but it just doesn't feel "right" because you're assigning a value to something outside of a function... so when does that value get assigned? During compile-time? At run-time?
I've done what you said and simply created an array of Tile's with 4 elements... Now i get the error
"Error 7 error C2148: total size of array must not exceed 0x7fffffff bytes c:\users\bryce\documents\visual studio 2008\projects\...\...\block.h 29"
My tile class certainly isn't that large... (half a terabyte if my calculations are correct!)
If I can't figure that out then I'll involve vectors >_>
EDIT: it may interest you to know that I also get the error "Error 2 error C2079: 'Block::TileArray' uses undefined class 'Tile' c:\users\bryce\documents\visual studio 2008\projects\...\...\block.h 29"
This is the problem I was coming across earlier and is the reason I thought it wasn't possible to do this. I have a forward declaration for the Block class right before my Tile class, but there is still no way for it to know it's definition. How can I overcome this?
modified on Tuesday, September 30, 2008 11:09 AM
|
|
|
|
|
|
It should then, be as simple as putting #include "Tile.h" at the top of Block.h but this still doesn't quite do it Those two errors remain.
|
|
|
|
|
That won't work if you also have #include "Block.h" at the top of "Tile.h" for example...
I really recommend throwing away most of the insides of those two classes until you narrow down the trouble.
Iain.
|
|
|
|
|
I've been thinking hard about this and the only thing I can think of that might cause this problem is my constructors (the default constructor and a second constructor that takes two parameters.
Block::Block(void)
{
Grounded = false;
Shape = (rand() % 7);
Colour = (rand() % 6);
p_TileArray = NULL;
p_TileArray = new Tile[4];
p_TileArray[0].SetPos(498.0, 82.0);
p_TileArray[1].SetPos(530.0, 82.0);
p_TileArray[2].SetPos(498.0, 114.0);
p_TileArray[3].SetPos(530.0, 114.0);
}
Block::Block(int itsShape, int itsColour)
{
Grounded = false;
Shape = itsShape;
Colour = itsColour;
p_TileArray = NULL;
p_TileArray = new Tile[4];
switch(Shape)
{
case SHAPE_O:
{
p_TileArray[0].SetPos(498.0, 82.0);
p_TileArray[1].SetPos(530.0, 82.0);
p_TileArray[2].SetPos(498.0, 114.0);
p_TileArray[3].SetPos(530.0, 114.0);
break;
}
case SHAPE_T:
{
p_TileArray[0].SetPos(530.0, 82.0);
p_TileArray[1].SetPos(498.0, 114.0);
p_TileArray[2].SetPos(530.0, 114.0);
p_TileArray[3].SetPos(562.0, 114.0);
break;
}
case SHAPE_L:
{
p_TileArray[0].SetPos(498.0, 114.0);
p_TileArray[1].SetPos(530.0, 114.0);
p_TileArray[2].SetPos(562.0, 114.0);
p_TileArray[3].SetPos(562.0, 82.0);
break;
}
case SHAPE_J:
{
p_TileArray[0].SetPos(498.0, 82.0);
p_TileArray[1].SetPos(498.0, 114.0);
p_TileArray[2].SetPos(530.0, 114.0);
p_TileArray[3].SetPos(562.0, 114.0);
break;
}
case SHAPE_S:
{
p_TileArray[0].SetPos(466.0, 82.0);
p_TileArray[1].SetPos(498.0, 82.0);
p_TileArray[2].SetPos(466.0, 114.0);
p_TileArray[3].SetPos(498.0, 82.0);
break;
}
case SHAPE_Z:
{
p_TileArray[0].SetPos(466.0, 82.0);
p_TileArray[1].SetPos(498.0, 82.0);
p_TileArray[2].SetPos(466.0, 114.0);
p_TileArray[3].SetPos(498.0, 82.0);
break;
}
case SHAPE_I:
{
p_TileArray[0].SetPos(466.0, 82.0);
p_TileArray[1].SetPos(498.0, 82.0);
p_TileArray[2].SetPos(466.0, 114.0);
p_TileArray[3].SetPos(498.0, 82.0);
break;
}
default:
{
break;
}
}
}
Block::~Block(void)
{
delete [] p_TileArray;
p_TileArray = NULL;
}
AFAIK the rest of the class doesn't matter for this problem so I'll leave it out.
The line I'm concerned with is p_TileArray = new Tile[4]; in both constructors. Only one constructor will ever be called for each instance of the class, so theoretically this shouldn't be a problem, but theory is rarely an actuality :P Is there any way this could be causing me problems? I've done a bit of fiddling around with that bit hoping to fix it by trial and error but still I've got nothing to show for the time I've invested in it
|
|
|
|
|
I've looked into the problem a bit further and the destructor for my Block object is being called from the following area of my UpdateWorld() function;
for(int i=0; i < 10; i++)<br />
{<br />
Block theBlock;<br />
p_CurrentBlock = &theBlock;<br />
vector_BlockList.push_back(*p_CurrentBlock);<br />
}
Every time the for loop iterates, theBlock falls out of scope and thus it's destructor is called. I've yet to work out why that would be a problem (and I doubt it is) but it may be helpful information to know
EDIT: Wow, I'm very stupid indeed :P
When theBlock falls out of scope, p_CurrentBlock becomes a wild pointer. It could be pointing to anything!
Another edit: Problem solved! Thankyou very much for your help Iain
Btw you have a very cool name
Yet another edit™: I seem to have jumped the gun. Commenting out the following code fixed it, but when I made the necessary changes to it I still get the same problem!
for(int i=0; i < 10; i++)<br />
{<br />
Block theBlock; <br />
vector_BlockList.push_back(theBlock);<br />
p_CurrentBlock = &vector_BlockList.back();<br />
}
Strangely, though THIS code, that is called elsewhere in my program, works fine.
if (p_CurrentBlock->isGrounded() == true)<br />
{<br />
Block theBlock;<br />
vector_BlockList.push_back(theBlock);<br />
p_CurrentBlock = &vector_BlockList.back();<br />
}
Commenting out the first code snippet makes it work, but commenting out the second one only doesn't, so it's not a matter of those two snippets conflicting with each other.
further edit:
if (p_CurrentBlock->isGrounded() == true)<br />
{<br />
Block theBlock;<br />
vector_BlockList.push_back(theBlock);<br />
p_CurrentBlock = &vector_BlockList.back();<br />
}
was always evaluating to false, and thus the statements were not executed. The culprit lies somewhere in
Block theBlock;<br />
vector_BlockList.push_back(theBlock);<br />
p_CurrentBlock = &vector_BlockList.back();
which most likely leads us back to the destructor for Tile
modified on Saturday, October 4, 2008 11:03 AM
|
|
|
|
|
Previous post is getting long, so I'm making another.
I noticed some strange behaviour that I believe is definitely the cause of the "vector deleting destructor" error I've been getting.
I set a break point on each of the following lines and a watch on p_TileArray[0] through p_TileArray[3].
In the watch window it only shows p_TileArray for each variable, without the index. Furthermore, when I step through the breakpoints, You can see that p_TileArray is being treated as a single variable, as with every call to SetPos() , each element is given the same value. That is why the destructor is failing, as it is a vector deleting destructor and is being used as a scalar deleting destructor.
What causes this behaviour with p_TileArray and how do I fix it?"
See image here[^]
|
|
|
|
|
Bloody hell this is getting annoying >_> I ought to not be posting at this time of night as I do stupid things.
Anyway, turns out it is being treated as a regular array, like it should.
It's probably worth noting that the constructor is being called twice by the code, which should not happen.
if (p_CurrentBlock->isGrounded() == true)<br />
{<br />
Block theBlock;<br />
vector_BlockList.push_back(theBlock);<br />
p_CurrentBlock = &vector_BlockList.back();<br />
}
and immediately after stepping out of the constructor for the second time, we get a crash / assert failure, and every element of p_TileArray loses its data and becomes -1.5883997e+038 >_>
EDIT: I really think I may have figured it out now - I haven't defined a copy constructor for my Block class, so when theBlock is copied into vector_BlockList and we exit the if statement, all the Tile objects created on the heap are deleted, whilst theBlock that was passed into vector_BlockList still has a pointer that refers to them.
I've yet to implement the copy constructor as it's nearly 2 AM over here, but it's first on the table for tomorrow. Here's hoping for a good day tomorrow :P
modified on Saturday, October 4, 2008 11:46 AM
|
|
|
|
|
Okay so now I've built the copy constructor and I'm fairly certain it works perfectly, however now when I step through the program, The constructor for Block is being called once, followed by it's destructor, which now exhibits normal behaviour, first deleting p_TileArray and then setting it to NULL . Then the constructor is being called a second time, once again followed by the destructor, which also exhibits normal behaviour, but THEN the destructor is called a second time without another call to the constructor, which ends with a Debug assertion failure
EDIT:
for(int i=0; i < 10; i++)<br />
{<br />
Block theBlock; <br />
vector_BlockList.push_back(theBlock);<br />
p_CurrentBlock = &vector_BlockList.back();<br />
}
After the second iteration of this loop, vector_BlockList contains two of my Block class. The watch window tells me that the member variables are:
vector_BlockList[0].Shape = 5
vector_BlockList[0].Colour = 2
vector_BlockList[0].Grounded = false
vector_BlockList[0].p_TileArray = 0x00d15c7c {left=-1.5883997e+038 top=-1.5883997e+038 depth=-1.5883997e+038 ...}
vector_BlockList[1].Shape = 1
vector_BlockList[1].Colour = 4
vector_BlockList[1].Grounded = false
vector_BlockList[1].p_TileArray = p_TileArray = 0x00d15c7c {left=-1.5883997e+038 top=-1.5883997e+038 depth=-1.5883997e+038 ...}
modified on Saturday, October 4, 2008 7:18 PM
|
|
|
|
|
finally solved
It was an error in my copy constructor for the Block and Tile classes. Because the copy constructor was const , it had to invoke const -only methods. Turns out I stuffed up the syntax and used
const int GetSomeVar()
{
return SomeVar;
}
instead of
int GetSomeVar() const
{
return SomeVar;
}
Thanks to everyone who helped.
|
|
|
|
|
Even in CEdit::PreTranslateMessage I can't meet WM_MOUSEHOVER .
No problem with CListCtrl ,
even without a call to TrackMouseEvent ,
and I'm not sure if TrackMouseEvent is necessary any more.
logics
|
|
|
|
|
WM_MOUSEHOVER is a relatively new mouse message, so I'm not surprised at all that it doesn't fire from an EDIT window (one of the golden oldies).
You could always write your own CIHandleHoveringEdit class, and handle WM_MOUSEMOVEs, etc, and a WM_TIMER you reset each time the mouse moves... and so on. Not trivial, but do-able.
Iain.
|
|
|
|
|
H havei,
I am having a CListCtrl control in my dialog box. The ClistCtrl Control is having 5 columns withe column heads.
Now what i want to do is on a button click i want the same ClistCtrl to have redraw itself withe 2 columns along with their column heading.
So what exactly i want is to dynamically change the number of columns of a ClistCtrl along with their
headings. My application is on MFC
Please tell me how to do it.
Thanks In advance.
Dhiraj
|
|
|
|
|
You can also check out the DeleteColumn API if it suits your needs.
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
Use the CListCtrl::DeleteColumn() function.
|
|
|
|
|
Hi all,
I want to write the content of a buffer into a stringstream. So I do the following.
HRESULT hrRead;
ULONG readLen = 0;
do
{
char writeBuffer[100] = {0};
hrRead = pOutFileStream->Read(writeBuffer, sizeof(writeBuffer), &readLen);
str.write(writeBuffer, readLen);
}while(hrRead == S_OK);
But it's write only one byte to the stream. I can't figure it out. Can you someone helps me to fix it.
Thanks
I appreciate your help all the time...
CodingLover
|
|
|
|
|
CodingLover wrote: pOutFileStream->Read(writeBuffer, sizeof(writeBuffer), &readLen);
I am not sure, but I guess if the string in the buffer is in UNICODE format, such problem will occur.
|
|
|
|
|
Actually pOutFileStream is a IStream (I mean CComPtr<istream>) in UNICODE. I want to that stream attach to a buffer.
I appreciate your help all the time...
CodingLover
|
|
|
|
|
Well in that case, after reading from the buffer, convert the UNICODE to multi byte( use WideCharToMultiByte API ) and then set it to stringstream.
|
|
|
|
|
I'm bit confusing now sir.
I have read a stream(pOutFileStream) into a buffer. And then write that buffer content into a ostream. Actually pOutFileStream is a stream of an XML file formatted in UNICODE.
If you have any example related to that conversion please send it here.
Thanks
I appreciate your help all the time...
CodingLover
|
|
|
|
|
try this..
HRESULT hrRead;
ULONG readLen = 0;
do
{
char writeBuffer[100] = {0};
wchar_t ReadBuffer[100] = {0};
hrRead = pOutFileStream->Read(ReadBuffer, 100 , &readLen);
WideCharToMultiByte( CP_ACP, NULL, ReadBuffer, -1,writeBuffer, 100, NULL, NULL );
str.write(writeBuffer, strlen(writeBuffer));
}while(hrRead == S_OK);
|
|
|
|
|
I try what you suggest. But no luck.
My xml stream like this, <state>0</state> but the buffer only contain < in my previous code. With your suggestion, nothing is contain.
Here is my full code.
int GetStat::CopyTo(ostream& str)
{
HRESULT hrSeek, hrRead;
ULONG readLen = 0;
LARGE_INTEGER startPos = {0};
if((hrSeek = pOutFileStream->Seek(startPos, STREAM_SEEK_SET, NULL)) != S_OK)
{
LOG4CPLUS_ERROR(RF_GROUP_LOGGER, "Stream seek failed." << __FUNCTION__);
return -1;
}
do
{
char writeBuffer[256] = {0};
wchar_t ReadBuffer[100] = {0};
hrRead = pOutFileStream->Read(writeBuffer, sizeof(writeBuffer), &readLen);
str.write(writeBuffer, readLen);
}while(hrRead == S_OK);
return 0;
}
std::string GetStat::GetAsString(void)
{
stringstream str(ios::in|ios::out|ios::binary);
if(CopyTo(str) == 0)
return str.str();
return "";
}
int GetStat::Result()
{
std::string temp = GetAsString();
std::cout << temp << std::endl;
char buff[256] = {0};
sprintf(buff, "%s", temp.c_str());
}
You can find the buff in above code. In Result() method you can see I've print the output to the console. It gives the correct xml stream. But buff contain only the first character.
I appreciate your help all the time...
CodingLover
|
|
|
|
|
I still belive this is because of the UNICODE. Did you debug and check the return value of WideCharToMultiByte() function, and if it is success what is the value in ReadBuffer , writeBuffer etc?
|
|
|
|
|