|
I have a c++ class that contains the following:
template<class T> class Poly {
public:
Poly<T>(int order = 0)
{
this->order = order;
coeffs = new double[order+1];
for( int i = 0;i<=order; i++ )
coeffs[i] = 0;
}
Poly<T>( int order, T *coeffs);
Poly<T>( Poly &poly );
~Poly()
{
delete []coeffs;
}
// other member functions are not shown.
friend ostream &operator << ( ostream &out,
Poly<T> &poly );
private:
// data members not show.
};
This class is defined in a file called poly.h. In the file poly.cpp, I have the following function definition:
template<class T>
ostream & operator << ( ostream &out, Poly<T> &poly )
{
bool printedCoeff = false;
.
.
.
return out;
}
In a third file called test.cpp, I have:
double coeffs[] = { 3, 1, 2, 1 };
Poly<double> p1(3,coeffs), p2;
cout << p1 << endl;
I am using the Microsoft Visual Studio and I get a linker error because of the statement:
cout << p1 << endl;
I believe that my definition of the function overloading << is
not properly getting instantiated. How do I fix this?
Bob
|
|
|
|
|
|
I am trying to use GDI+ within my MFC dialog based application with multiple child dialogs. On one of the child dialogs, I am trying to draw an image from an array of 8 bit BYTE pixel data. Here is how my flow process is for this-
Variables declared in my child dialog's header file.
LONG DIBSecWidth;
LONG DIBSecHeight;
LONG BitsPerPixel;
LONG Stride;
BITMAPINFO* bmi;
BYTE* pDIBSectionBits;
HBITMAP hbm;
Graphics* imagedisp;
Bitmap* offscreenBitmap;
RECT m_imagedisp_rect;
BOOL m_BUpdate;
Then in my OnInitDialog() function, I do the following
DIBSecWidth = 512;
DIBSecHeight = 512;
BitsPerPixel = 8;
Stride = ((DIBSecWidth* BitsPerPixel + 31L) & (~31L)) / 8L;
bmi = (BITMAPINFO *)new BYTE[sizeof(BITMAPINFO) + UCHAR_MAX * sizeof(RGBQUAD)];
bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi->bmiHeader.biWidth = DIBSecWidth;
bmi->bmiHeader.biHeight = DIBSecHeight;
bmi->bmiHeader.biPlanes = 1;
bmi->bmiHeader.biBitCount = (WORD)BitsPerPixel;
bmi->bmiHeader.biCompression = BI_RGB;
bmi->bmiHeader.biSizeImage = 0;
bmi->bmiHeader.biXPelsPerMeter = 0;
bmi->bmiHeader.biYPelsPerMeter = 0;
bmi->bmiHeader.biClrUsed = 0;
bmi->bmiHeader.biClrImportant = 0;
memcpy(bmi->bmiColors, summercmap, 256*sizeof(RGBQUAD));
hbm = ::CreateDIBSection(NULL, bmi, DIB_RGB_COLORS, (void**)&pDIBSectionBits, NULL, 0);
offscreenBitmap = new Bitmap(bmi, pDIBSectionBits);
ImageDisplay.GetClientRect(&m_imagedisp_rect);
imagedisp = new Graphics(ImageDisplay.GetDC()->GetSafeHdc());
m_BUpdate = true;
and in OnPaint() message handler I have the following
CPaintDC dc(this);
if (m_BUpdate)
{
BYTE *pCurRowPixel = (BYTE *)(pDIBSectionBits);
memcpy(pCurRowPixel, m_pImgBuffPrc, DIBSecWidth*DIBSecHeight);
imagedisp->DrawImage(offscreenBitmap, 0, 0, DIBSecWidth, DIBSecWidth);
m_BUpdate = false;
delete pCurRowPixel;
}
Whenever I run my application, it throws an error at imagedisplay->DrawImage function, if I comment it out it doesn't throw the error. I am sure there is something simple that I am missing here or the way I am trying to draw is not right, any ideas/suggestions?
thanks
PKNT
modified 21-Sep-15 13:49pm.
|
|
|
|
|
Quote: delete pCurRowPixel;
What's the purpose of the above line? It looks plain wrong, to me.
|
|
|
|
|
I saw that too. But it is guarded by m_BUpdate .
|
|
|
|
|
The crashing call too is guarded by m_BUpdate .
Anyway, it is wrong calling delete on the memory allocated via CreateDIBSection .
|
|
|
|
|
CPallini wrote: Anyway, it is wrong calling delete on the memory allocated via CreateDIBSection .
Which might be the reason for the exception because <code> _BLOCK_TYPE_IS_VALID is thrown when trying to delete memory that wasn't allocated using new .
|
|
|
|
|
Is there any possibility to catch shift + left key without mess up PreTranslateMessage ? I have tried this:
void CGridCtrlExt::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if(VK_LEFT == nChar || VK_RIGHT == nChar)
TRACE1(">>>%d", nChar);
CGridCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
}
I see TRACE message ... but when I wrote:
void CGridCtrlExt::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if((VK_LEFT == nChar || VK_RIGHT == nChar) && GetKeyState(VK_SHIFT) < 0)
TRACE1(">>>%d", nChar);
CGridCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
}
I saw nothing ... can you help me out, please ? Thank you.
|
|
|
|
|
It should work that way. But it will not work if those key combinations are "eaten" by an existing pre-translation. You may test that by using the first version with an extended trace:
TRACE1(">>>%d, state %x", nChar, GetKeyState(VK_SHIFT));
If the above is not called with shift key down, it is probably "eaten". Then you must use PreTranslateMessage or try to disable the existing translation.
|
|
|
|
|
I was just made the same test:
TRACE("%d|%d\n", IsSHIFTpressed(), nChar);
but no, with shift key pressed, the keydown handler seem not calling ... I have to see what happen above this class. Thank you, and I'll come back with feedback.
|
|
|
|
|
Strange ... I have checked if parent of CGridCtrlExt (CGridCtrl ), has PreTranslateMessage , but no, has not ... more over, I override CGridCtrlExt::OnChar , CGridCtrlExt::OnSysChar , CGridCtrlExt::OnSysKeyDown , none of them tell me when I press the shift key ... weird ... I don't know where to search the issue ...
|
|
|
|
|
I don't know which CGridCtrl you are using. The keys may be also catched by an underlying Windows control so that you don't see the translator source.
If there is some action associated, you should recognize it. What happens when pressing Shift Left/Right on a wide item?
You may spend some more time for investigation or just implement your own PreTranslateMessage handler.
|
|
|
|
|
"I don't know which CGridCtrl you are using"
I am using this[^] one.
|
|
|
|
|
I have not used that so far. But I'm quite sure that the arrow keys are used to select cells when the Shift key is pressed too.
|
|
|
|
|
I guess it is my mistake !!! : I already have an accelerator, shift + left key, on menu item command ... if this is the cause, is there any possibility to know in CGridCtrlExt, of on CDialog which contain this control, to know when I pressed Shift and left key ?
|
|
|
|
|
If you have a global accelerator you have three options:
- From the accelerator handling check if your grid control is active and pass the message to that if so
- Add another accelerator table to your grid control
- Use PreTranslateMessage in your grid control
|
|
|
|
|
I will try your first solution. I'll come back.
|
|
|
|
|
Because left/right key combined with shift/ctrl key are used to mark grid cells, I have chose to use F7/F8 key for my needs. Thank you all of you for patience and attention. Bye.
|
|
|
|
|
I have just tried this and it all works as per the documentation. I can only assume that these keys are being trapped by the CGridCtrlExt control somehow.
|
|
|
|
|
Neither CGridCtrlExt has PreTranslateMessage , or something that could trap the shift key ... I can not understand what happen ...
|
|
|
|
|
Do you have the source of that class to check? The only real answer is to use your debugger and inspect all the values that are set when the key combination is pressed.
|
|
|
|
|
I think I found the problem:
[^].
|
|
|
|
|
Apparently compiler does not like to have both constructor and class variable names <b>to be same</b>.
this->variable_name = variable_name
I am using "this" pointer to pass / copy parameters passed to class constructor so they can be accessed by class methods.
I have managed to do it for simple int
Here is the declaration of int disp_x_size_, disp_y_size_; in the class
int disp_x_size_, disp_y_size_; //size x y
int *ControlPin; // control pins array
int *DB; // data bus pins array
and here is how I use "this" pointer in the constructor :
this->disp_x_size_ = disp_x_size_; // class local variable = paramater passed to constructor
works fine.
Now I tried to do same for passed pointer - for ControlPin declaration see above and here is the "this" pointer code
this->ControlPin = ControlPin;
this->DB = DB;
Compiles OK , but when I try to use it all I am getting is the actual content of the first member of the array. I have limited ways to check the pointer.
When I try to actually use it in this function
LCD_Write_COM_DATA_t(ControlPin, DB , (uint32_t)0x44, (uint32_t) ((x2 << 8) + x1),16); // horizontal RAM address position
it does not compile with the following error
C:\DOCUME~1\Vaclav\LOCALS~1\Temp\build5821005168044960715.tmp\sketch\A.h: In member function 'void TFT_LCD::setXY(word, word, word, word)':
C:\DOCUME~1\Vaclav\LOCALS~1\Temp\build5821005168044960715.tmp\sketch\A.h:9297:87: error: no matching function for call to 'TFT_LCD::LCD_Write_COM_DATA_t(<b>int*&, int*&,</b> uint32_t, uint32_t, int)'
LCD_Write_COM_DATA_t(ControlPin, DB , (uint32_t)0x44, (uint32_t) ((x2 << 8) + x1),16); // horizontal RAM address position
I believe that instead of simple int* I am passing the contents of the first array member int*&
which obviously is not the TYPE expected by the template function.( And I can verify that)
BTW that function works when correct TYPE pointer is passed to it.
I basically need someone to explain to me what am I doing wrong using "this" pointer that I end up with int*& instead of just int*.
I hope it makes sense, if not I can explain it more. But don't ask for full code - it is now over 10000 lines of giant mess
Appreciate any help, as always
Cheers
Vaclav
-- modified 20-Sep-15 11:25am.
|
|
|
|
|
Vaclav_Sal wrote: what am I doing wrong using "this" Nothing. this is merely a pointer to the data space of 'this' object, when in the code of that object, i.e. inside the methods declared in the class. Using it in the constructor has no relevance to your problem. The problem is that your call to LCD_Write_COM_DATA_t contains parameters that do not match the declaration of the method.
Taker a look at Pointer to Pointer and Reference to Pointer[^] for an explanation.
|
|
|
|
|
"contains parameters that do not match the declaration of the method."
Sorry, but it is just the opposite.
The method is OK, it's the wrong parameters I am passing in and I really do not know why.
I do not want contents , I need pointer to the array.
If I change the method I would have to rebuild it so it will take a contents, not pointer.
I'll take a look at the reference, thanks you for that.
|
|
|
|
|