|
I'm trying to override vector's operator[] but am getting multiple errors. I have an array object derived from vector. The operator[] of vector does not check for an invalid object and always returns a pointer of the object type stored in the object regardless of whether or not that object actually exists in the array. MSDN says "If that position is invalid, the behavior is undefined". Ok, I'll buy that but that causes alot of problems in my programs that will use it cuz then I'd have to check for invalid parameters each time I call it when I'd rather do the check in the array of type vector object itself.
For example, if I have 3 objects in my derived array class and ask it to return a pointer to the 4th object in the array, or ask it to return a reference to the -1 element, it will. Slightly modified example below:
class CMyArray : public vector<CMyObject>
{
public:
CMyArray() : vector<CMyObject>()
{
}
};
I added the following code to above but get several errors, of course.
CMyObject& operator[](size_type pos)
{
if(pos < 0)
return NULL;
if(pos > size())
return NULL;
return vector::operator[pos];
};
The errors for the two if's returning NULL are:
C:\Scratch Code\myobject.h(172) : error C2440: 'return' : cannot convert from 'const int' to 'class CMyObject&'
Which I expect but really don't no what else to return to indicate an invalid index, object, or position. As for the last statement, I've gotten different errors depending on how I coded it. Don't know what's right. I've tried vector::operator[](pos) but gives me error about "ambigious call to overloaded function."
Thanks
|
|
|
|
|
NULL is a pointer, not a reference to CMyObject , so it's no wonder the compiler complains. Your only option here (to the best of my knowledge) is to throw an exception.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
You might consider using the "at" function in vector instead. That function does bounds checking. That way you wouldn't have to do any specialization. "at" may throw so be sure to put the call in a try .. catch block.
PS. When checking your bounds as in your snip-it above, I think you want (pos >= size()) as your second fail condition since size() is 1 too many.
|
|
|
|
|
You're right about the (pos >= size()). I looked into at() and it looks like it will work fine. Thanks, guys!
|
|
|
|
|
Hi all!
I want to create a large bitmap, draw on it and then just display a portion of it (there is a scrolling feature in my app).
When I use the code below and set the 'picwidth' to i.e. 1000, it works just fine.
But when I set the width to i.e. 5000 just a black bitmap appears, without the white line.
There is NO error at 'CreateCompatibleDC' and 'CreateCompatibleBitmap' !
CBitmap bmp;
CDC dc2;
CClientDC dc(this);
int picwidth = 5000;
if (!dc2.CreateCompatibleDC(&dc)) {
AfxMessageBox("error");
return FALSE;
}
if (!bmp.CreateCompatibleBitmap( &dc, picwidth,512)) {
AfxMessageBox("error");
return FALSE;
}
CBitmap* oldbmap=dc2.SelectObject(&bmp);
CPen pen( PS_SOLID, 0, RGB(255,255,255));
CPen* pPenOld = dc2.SelectObject( &pen );
int nMap = dc2.SetMapMode( MM_TEXT );
for (i=0;i<picwidth;i++) {
dc2.MoveTo( i, 300 );
dc2.LineTo( i, 300 );
}
dc2.SetMapMode( nMap );
dc2.SelectObject( pPenOld );
pDC->BitBlt(5,5, 600 , 512 ,&dc2,0,0,SRCCOPY);
dc2.SelectObject(oldbmap);
Is there anything wrong in my code ?
What can I do that the 5000 pixel wide bitmap is created successfully ?
Thank you very much in advance!
Best regards,
Hannes / Austria.
|
|
|
|
|
There *is* a physical limit to bitmap size, and I reckon you've crossed it.
Christian
No offense, but I don't really want to encourage the creation of another VB developer.
- Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael
P Butler 05-12-2002
It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003
|
|
|
|
|
what's the return value of the SelectObject?
-c
Be very, very careful what you put into that head, because you will never, ever get it out. --Thomas Cardinal Wolsey
|
|
|
|
|
Hi Chris!
Which one do you mean ?
The last one ?
dc2.SelectObject(oldbmap);
..with
szMsg.Format(" %d", dc2.SelectObject(oldbmap));
AfxMessageBox(szMsg);
I get: 61047404
Thank you,
Hannes.
|
|
|
|
|
the first one, the one that selects the huge bitmap into the DC.
-c
Be very, very careful what you put into that head, because you will never, ever get it out. --Thomas Cardinal Wolsey
|
|
|
|
|
Good morning
The return values for
CBitmap* oldbmap=dc2.SelectObject(&bmp);
are:
4423188 if the bitmap creation succeeds (width = 1000)
4423204 if something goes wrong (width = 5000)
Thank you very much!
Best regards,
Hannes.
|
|
|
|
|
what is the return value from the BitBlt ?
-c
Be very, very careful what you put into that head, because you will never, ever get it out. --Thomas Cardinal Wolsey
|
|
|
|
|
Hi Chris!
Just came home from work...
I checked the return values from BitBlt and I get a 1 in both cases.
It's not surprising since the bitmap is actually drawn, but it is just black. (without the white line)
Thank you very much for working on this with me!
Very much appreciated!!!
Best regards,
Hannes / Austria.
|
|
|
|
|
Hi:
I have a matrix class and I would like to be able to fill in the elements by supplying a list of values. In other words, I would like to be able to write something like this:
matrix A(2, 2) = {3, 4; 2, 1};
This would create a 2 x 2 matrix:
3 4
2 1
Is this possible? The only workaround that I can think of right now is to convert the right hand side to a string "3, 4; 2, 1" and then overload the assignment operator with something like this:
matrix& operator = ( char *sString );
And then do a whole bunch of splits, trims, and searches within the string.
Can anyone suggest a better solution?
Thanks
|
|
|
|
|
of course it's possible!
int a[2][2] = { {1,2} , {3,4} };
#include <beer.h>
|
|
|
|
|
The thing is that I am assigning this array to a class, which has variables other than the 2-dimensional array or numbers (I don't think shallow assignment will do here):
class matrix
{
private:
double **_m;
double *_v;
int _nType, _nRows, _nCols;
void _Copy( const matrix &m1 );
void _Destroy( );
public:
// Default constructor
matrix( int nMatrixType = mxMatrix, int nDim1 = 0, int nDim2 = 0 );
// Copy constructor
matrix( const matrix &m1 );
~matrix( );
matrix& operator = ( const matrix &m1 );
matrix operator * ( const matrix &m1 );
int GetType( ) { return _nType; }
int GetRows( ) { return _nRows; }
int GetCols( ) { return _nCols; }
void Display( ) const;
double GetElement( int nDim1, int nDim2 = 0 ) const;
void SetElement( double fValue, int nDim1, int nDim2 = 0 );
matrix Transpose( );
matrix Solve( const matrix &mRHS );
};
|
|
|
|
|
Does your design require dynamic allocation? In other words, does the program dynamically allocate matrices during run-time? If not, one solution is an STL container such as a list of paired values.
Kuphryn
|
|
|
|
|
The program DOES allocate matrices dynamically; however, there is going to be a fixed 9 x 9 matrix with predetermined values that is going to be used in calculations (this matrix needs to be of the same type (class) as the other dynamically allocated matrices). I just want to avoid typing the following 81 times:
m.SetElement(0, 0, 100.0);
m.SetElement(0, 0, 200.0);
.....
and replace it with something like m = { 100.0, 200.0, ... }
Thanks.
|
|
|
|
|
One solution for predefined values is a loop.
Kuphryn
|
|
|
|
|
How about using varargs..?
define a member function in the class which loads the values from a vararg list...
you loose type checking, and will have to indicate the number of arguments somehow...
Blade[DMS]
|
|
|
|
|
I have :
int te = submenu->GetMenuItemCount();
for (int i = 0; i < te; i++)
{
submenu->DeleteMenu(0, MF_BYPOSITION);
}
This works okay.
Upto now I had:
int te = submenu->GetMenuItemCount();
for (int i = 0; i < submenu->GetMenuItemCount(); i++)
{
submenu->DeleteMenu(0, MF_BYPOSITION);
}
This loop was quitting when i reached 16, and all the items werent being deleted. All I did was put in <code>te </code> the variable instead and it loops through all 31. I am confused, because it required a lot of debugging and I was not at all suspecting something like this....
What am I missing?
Appreciate your help,
ns
|
|
|
|
|
you loop up until variable i is greater or equal than number of items left...
while you increase i , the number of GetMenuItemCount() shrinks and 16 is the "magic" meeting point.
here is an alternative:
while(submenu->GetMenuItemCount>0) submenu->DeleteMenu(0, MF_BYPOSITION);
Greets, Moak
|
|
|
|
|
being picky, cos i like slim (obfuscated?!?!) code:-
while(submenu->GetMenuItemCount())
submenu->DeleteMenu(0, MF_BYPOSITION);
no need for ">0"
#include <beer.h>
|
|
|
|
|
MSDN says the method returns -1 on error, thats why I tested >0 .
-1 would be rated TRUE and so would cause an endless loop (in your code)?
|
|
|
|
|
Thanks ! I see.....
Appreciate your help,
ns
|
|
|
|
|
Hi there,
can I draw items of a listbox into a given DC?
I have a rectangle that's an area inside a listbox. Now I need to repaint all corresponding listbox items into given DC (will be a memory DC later for flicker free bitblitting). In the past I simply called:
pMyListBox->InvalidateRect(rect);pMyListBox->UpdateWindow();
...now I want to draw inside the given DC.
How do I call CListBox::DrawItem() manually, or any better alternatives?
Thx for help, Moak
|
|
|
|
|