|
Thank you again you know I’m an assembler mail framer by birth when I post any question on IBMMAIN no matter how much research I have done I get screamed at wish IBMMAIN could be more like the codeproject thank.
Time for to implement my code or actually yours
|
|
|
|
|
<pre>in my applications need some scanner support ,I thought it is a good idea to get into TWAIN and try it out.after that I had an exit error that is driving me nuts as an Debug Assertion Failed....
in cmdtarg.cpp line 40 CCmdTarget::~CCmdTarget because m_dwRef is greater than 1 (in this case it is 2)
|
|
|
|
|
|
I working in an embedded environment (STM32 microcontroller) and I compile using GCC (STM32CubeIDE). I want to put a C++ object inside a struct and this is the only way I could get it to compile:
namespace MyNamespace {
class MyObject
{
int m_i;
public:
MyObject(int i):m_i(i){}
MyObject(const MyObject&) = delete;
};
}
typedef struct {
MyNamespace::MyObject* MyObject;
} MyStruct_s;
static MyStruct_s MyStruct = {new MyNamespace::MyObject(0) }; However, I'm having strange hanging issues in my code and I suspect the code above might be the cause. Is the code above legal or will it corrupt RAM? If so, what's the correct way to do it? Ideally I'd like to have the whole object inside the struct, not just a pointer to it.
modified 5-Dec-22 8:23am.
|
|
|
|
|
As matter of fact, you don't need a pointer. Try
#include <iostream>
using namespace std;
namespace MyNamespace
{
class MyObject
{
int m_i;
public:
MyObject(int i):m_i(i){}
int get_i(){return m_i;}
};
}
struct MyStruct
{
MyNamespace::MyObject myObject;
};
static MyStruct s_myStruct = { MyNamespace::MyObject(5) };
int main()
{
cout << s_myStruct.myObject.get_i() << endl;
}
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
I'm able to compile your code example. However, I just realized that I had forgotten the following line of code (I didn't think it was important) in my first post (I have added it now):
MyObject(const MyObject&) = delete; I'm not good at programming C++ so I'm not sure what that line does. After I have added this line, I get the following error message with your code example: "use of deleted function 'MyNamespace::MyObject::MyObject(const MyNamespace::MyObject&)"
|
|
|
|
|
|
I'm not allowed to make changes inside the class, I'm only supposed to write code that uses that class.
|
|
|
|
|
The line prohibits compiler from copying MyObject objects.
Look at the following code to better understand what's going on:
namespace MyNamespace
{
class MyObject
{
int m_i;
public:
MyObject (int i) :m_i (i + 1) { cout << "MyObject constructor i=" << i << endl; }
MyObject (const MyObject&) = delete; int get_i () const { return m_i; }
};
}
struct MyStruct
{
MyStruct (const MyNamespace::MyObject& r) : myObject (r.get_i ()) { cout << "MyStruct constructor" << endl; }
MyNamespace::MyObject myObject;
};
static MyStruct s_myStruct { MyNamespace::MyObject (5) };
int main ()
{
cout << s_myStruct.myObject.get_i () << endl;
} The output is:
MyObject constructor i=5
MyObject constructor i=6
MyStruct constructor
7
Mircea
|
|
|
|
|
Fine.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Your code is correct but dangerous. You don’t control the time when myStruct is initialized. There are various methods to control it. My preferred method is the nifty counter[^].
In general, you can include objects in structures. As a matter of fact C++ makes little difference between the two. Without seeing the code, I suspect that your problem is that you don’t have a default constructor for your object. Let me explain: as you don’t have a default constructor for myStruct , the compiler will synthesize one that invokes the default constructor for any included non-trivial members. If your object doesn’t have a default constructor, the compiler complains.
Mircea
|
|
|
|
|
I edited my initial post to also show the actual class.
|
|
|
|
|
Good Morning.
In my view class in the onDraw() function, I am drawing the board and filling in the squares as follows:
pDC->SetMapMode(MM_LOENGLISH);
pDC->SetWindowExt(800, 800);
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
COLORREF color = pBC->getSquare(i, j);
CBrush brush(color);
int x1 = (j * 70) + 35;
int y1 = (i * -70) - 35;
int x2 = x1 + 70;
int y2 = y1 - 70;
CRect rect(x1, y1, x2, y2);
pDC->FillRect(rect, &brush);
}
}
for (int x = 35; x <= 595; x += 70)
{
pDC->MoveTo(x, -35);
pDC->LineTo(x, -595);
}
for (int y = -35; y >= -595; y -= 70)
{
pDC->MoveTo(35, y);
pDC->LineTo(595, y);
}
This all works well, and I have my board coming up.
Now I want to draw the chess pieces on the board, and have written some code, just to load 1 bitgmap and display it on the screen. However, nothing gets drawn to the screen. (All I see is just the chessboard)
This is the test code to display my bitmap on the screen:
void CMyChessTestView::drawImage(CDC* pDC, int x, int y)
{
CPaintDC dc(this);
CBitmap BmpLady;
CDC MemDCLady;
BmpLady.LoadBitmap(IDB_BISHOP_WHITE);
MemDCLady.CreateCompatibleDC(pDC);
CBitmap *BmpPrevious = MemDCLady.SelectObject(&BmpLady);
pDC->BitBlt(20, 10, 436, 364, &MemDCLady, 0, 0, SRCCOPY);
pDC->SelectObject(BmpPrevious);
The bitmap is not drwan to the screen. I have tried using both the CPaintDC dc or the pDC* but to no avail. I cannot get the bitmap to display.
Any suggestions will be greatly appreciated.
|
|
|
|
|
How do you call the drawImage method?
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
drawImage(pDC, 100, 100);
|
|
|
|
|
BigSteve-O wrote: drawImage(pDC, 100, 100);
And where are you calling this method from?
|
|
|
|
|
From the onDraw() funtion.
|
|
|
|
|
A similar code draws the bitmap, on my system. Why didn't you check the return value of the LoadBitmap call?
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
It looks like loadBitmap returns NULL. I have no idea as to why though.
Do you have a code snippet you can share?
|
|
|
|
|
BigSteve-O wrote: It looks like loadBitmap returns NULL. I have no idea as to why though.
According to Docs in CBitmap Class | Microsoft Learn :
Quote: If the bitmap identified by lpszResourceName does not exist or if there is insufficient memory to load the bitmap, the function returns 0.
|
|
|
|
|
|
|
Thank You.
I copied the sample code over, and I have noticed the following:
1) If I draw the bitmap first, and then draw the board afterwards, I can see the board and the test bitmap.
2) If I draw the board first and then the test bitmap, I only see the board. The loadBitmap call is successfull)
|
|
|
|
|
That's rather strange. Do you use the same DC for the board and bitmap?
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|