|
I am currently trying to create a program to find all possibilities for placing 5 queens and 5 knights on an 8x8 board in such a way nothing will attack anything else. There are 16 possible solutions. My problem i am encountering, is when placing queens. It is not generating all possibilities. I believe it is just taking the first instance it finds and going with that rather than examining the rest of the row for possible solutions. Here is what I have, if you could possibly help it would be greatly appreciated.
#include <iostream>
#include <string>
using namespace std;
class QueenKnight
{
public:
QueenKnight()
{
queens = 0;
knight = 0;
row = 8;
column = 8;
count = 0;
for(int i= 0; i<row; i++)
="" {
="" for(int="" j="0;" j<column;="" j++)
="" board[i][j]="." ;
="" }
=""
="" void="" updateboardq(int="" r,="" int="" c)
="" i="0;" i<row;="" if(i="=" r)
="" if(j="" !="c)
" if(="" (i-j)="=" (r-c)="" )
="" j!="c)
" (i+j)="=" (r+c)="" placequeen(int="" startr,="" startc)
="" if(board[startr][startc]="=" ".")
="" board[startr][startc]="Q" updateboardq(startr,="" startc);
="" if(board[i][j]="" if(queens="" <="" 5)
="" updateboardq(i,j);
="" queens++;
="" }
="" countfree()
="" 8;="" j<8;="" count++;
="" return="" count;
=""
="" displayboard()
="" i<="" row;="" cout="" <<board[i][j]="" <<"="" ";
="" <<endl;
="" bool="" checkknight(int="" i,int="" j)="" returns="" false="" if="" position="" is="" not="" acceptable
="" if((i+2)="" &&="" (j-1)="">=0)
{
if(board[(i+2)][(j-1)] == "Q" || board[(i+2)][(j-1)] == "K")
{
return false;
}
}
if((i+2) <= 7 && (j+1) <=7)
{
if(board[(i+2)][(j+1)] == "Q" || board[(i+2)][(j+1)] == "K")
{
return false;
}
}
if((i-2) >= 0 && (j-1) >=0)
{
if(board[(i-2)][(j-1)] == "Q" || board[(i-2)][(j-1)] == "K")
{
return false;
}
}
if((i-2) >= 0 && (j+1) <= 7)
{
if(board[(i-2)][(j+1)] == "Q" || board[(i-2)][(j+1)] == "K")
{
return false;
}
}
if((i+1) <= 7 && (j-2) >=0)
{
if(board[(i+1)][(j-2)] == "Q" || board[(i+1)][(j-2)] == "K")
{
return false;
}
}
if((i+1) <= 7 && (j+2) <=7)
{
if(board[(i+1)][(j+2)] == "Q" || board[(i+1)][(j+2)] == "K")
{
return false;
}
}
if((i-1) >= 0 && (j-2) >=0)
{
if(board[(i-1)][(j-2)] == "Q" || board[(i-1)][(j-2)] == "K")
{
return false;
}
}
if((i-1) >= 0 && (j+2) <= 7)
{
if(board[(i-1)][(j+2)] == "Q" || board[(i-1)][(j+2)] == "K")
{
return false;
}
}
return true;
}
void placeKnight()
{
for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
{
if(board[i][j] == ".")
{
if(checkKnight(i,j))
{
board[i][j] = "K";
knight++;
}
}
}
}
}
int knightCount()
{
return knight;
}
private:
int row;
int queens;
int column;
string board[8][8];
int count;
int knight;
};
int main()
{
int displays = 0;
for(int k=0; k<64; k++)
{
QueenKnight example;
example.placeQueen((k/8), (k % 8));
if(example.countFree() > 4)
{
example.placeKnight();
//cout <<"display: "<<displays<< endl;
="" if(example.knightcount()=""> 2)
// {
example.displayBoard();
cout <
|
|
|
|
|
Hi,
I did not study your code in detail, but have two remarks.
your code would be (more) readable when you:
- show it inside PRE tags (they preserve formatting, in particular indentation)
- (if possible) put opening brackets on the right of the previous line (which most
people don't do, I prefer it since it allows for more useful code lines per screen)
your code would be a lot simpler if you would use a one-dimensional board[64] array
(i.e. concatenating the eight rows) it would avoid a lot of for loops.
For attack checking, you would have to convert the linear index into a row/col pair
which is simply a divide/modulo 8 operation (two preprocessor macros can come in handy here).
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- use PRE tags to preserve formatting when showing multi-line code snippets
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
See here.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
can someone post a code on this forum that that takes a matrix converts it to upper triangular form prints the upper triangular form,
|
|
|
|
|
To post this code the users of forum will need to make the same as you can do by yourself... google, because I think no one is going to write you the whole code.
This link[^] is about multipliying matrix and this one[^] is about inverting order (i dont know the technical name) i mean A[i][j] -> A [j][i]. They are on spanish, but you can read the code examples and take a look int the figures, it should be not difficult to get the idea to programm it.
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
I wasn't sure where to post this, but since the code I'm having problems on is in my MFC app, I'm posting it here.
I have an RDTSC function in my code. It uses the RDTSC instruction to obtain a value in EDX:EAX. I need to print that value out before the function exits. How do I do that?
(I realize that this might be a stupid question, since I've only been working in assembly for around 3 months and I'm still learning.)
Thanks in advance.
|
|
|
|
|
Something like this maybe?
unsigned __int64 rdtscval;
__asm
{
rdtsc
lea ebx, rdtscval
mov [ebx], eax
mov [ebx+4], edx
}
CString str;
str.Format(_T("%I64u"), rdtscval);
::MessageBox(*this, str, _T("rdtsc"), MB_OK);
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I've written into some Code Project sample code a solution generously offered to me by a fellow poster for an adaptation to the code I had wanted to make. I had asked the board if there was a way to get at the value inside a variable called strContent here inside of OnDataReady (and one aspect of the solution involved renaming it to m_strContent, the way it's now shown here in this CMemMapCppClientDlg.cpp snip):
LRESULT CMemMapCppClientDlg::OnDataReady(WPARAM, LPARAM) <br />
{<br />
HANDLE hMapFile = NULL;<br />
PVOID pView = NULL;<br />
<br />
hMapFile = OpenFileMapping(FILE_MAP_READ, FALSE, m_pszMemMapFileName);<br />
if(hMapFile == NULL) {<br />
MessageBox("Can not open file mapping");<br />
return 0;<br />
}<br />
<br />
pView = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);<br />
if(pView == NULL) {<br />
MessageBox("Can map view of file");<br />
CloseHandle(hMapFile);<br />
return 0;<br />
}<br />
<br />
LPSTR szContent = reinterpret_cast<LPSTR>(pView);<br />
int nLen = strlen(szContent);<br />
while(nLen > 0) {<br />
m_strContent += *szContent++;<br />
--nLen;<br />
}<br />
m_strContent += '\0';<br />
m_strContent.Replace("\n", "\r\n");<br />
<br />
if(pView) UnmapViewOfFile(pView);<br />
if(hMapFile) CloseHandle(hMapFile);<br />
<br />
return 0;<br />
}<br />
<br />
CString CMemMapCppClientDlg::GetContent()<br />
{<br />
return m_strContent;<br />
}
Another part of the solution involved adding the ::GetContent() function, shown there immediately above.
Then the appropriate changes were shown to me for the header, here:
<br />
#if !defined(AFX_MEMMAPCPPCLIENTDLG_H__A51F6AF7_F28D_461D_8FB3_EFC7B929D99C__INCLUDED_)<br />
#define AFX_MEMMAPCPPCLIENTDLG_H__A51F6AF7_F28D_461D_8FB3_EFC7B929D99C__INCLUDED_<br />
<br />
#if _MSC_VER > 1000<br />
#pragma once<br />
#endif // _MSC_VER > 1000<br />
<br />
<br />
class CMemMapCppClientDlg : public CDialog<br />
{<br />
public:<br />
static UINT UWM_DATA_READY;<br />
CMemMapCppClientDlg(CWnd* pParent = NULL);
CString GetContent();<br />
<br />
enum { IDD = IDD_MEMMAPCPPCLIENT_DIALOG };<br />
protected:<br />
<br />
protected:<br />
virtual BOOL OnInitDialog();<br />
afx_msg LRESULT OnDataReady(WPARAM wParam, LPARAM lParam);<br />
DECLARE_MESSAGE_MAP()<br />
CString m_strContent; <br />
<br />
private:<br />
LPCTSTR m_pszMemMapFileName;<br />
};<br />
<br />
#define UWM_DATA_READY_MSG _T("UWM_DATA_READY_MSG-{7FDB2CB4-5510-4d30-99A9-CD7752E0D680}")<br />
<br />
<br />
#endif // !defined(AFX_MEMMAPCPPCLIENTDLG_H__A51F6AF7_F28D_461D_8FB3_EFC7B929D99C__INCLUDED_)
And, lastly, here is the MemMapCppClient.cpp that contains the call to GetContent():
...<br />
<br />
VARIANT _stdcall SumOneToArray(VARIANT sourceArray)<br />
{<br />
CMemMapCppClientDlg dlg;<br />
CString strContent = dlg.GetContent();<br />
MessageBox(NULL, _T(strContent), _T("Like this:"), MB_OK);<br />
<br />
if(sourceArray.vt==VT_DISPATCH)<br />
sourceArray = CheckExcelArray(sourceArray);<br />
<br />
long ncols, nrows, i, j;<br />
<br />
ncols=(sourceArray.parray)->rgsabound[0].cElements;<br />
nrows=(sourceArray.parray)->rgsabound[1].cElements;<br />
<br />
...<br />
}<br />
...
I've gone and stuck the...
CMemMapCppClientDlg dlg;<br />
CString strContent = dlg.GetContent();<br />
MessageBox(NULL, _T(strContent), _T("Like this:"), MB_OK);
...inside the top of one of the functions. The project won't compile if I put those statements anywhere else in the function, like in the middle. I get "initialization" errors.
The question is this: Even when all of this compiles perfectly, and my program works great--even the MessageBox gets displayed on cue--no content is there in the MessageBox. Never in any incarnation of a successful compile do I get content within the MessageBox. The prog always works perfectly otherwise.
(I know for a fact content is otherwise available in m_strContent over in the MemMapCppClient.cpp because if I put the MessageBox over there, I get m_strContent just fine.)
I botched this somehow. How can I get a value into the MessageBox inside of CMemMapCppClientApp?
Thanks again for all your help.
-- modified at 18:45 Thursday 8th November, 2007
|
|
|
|
|
e40s wrote: So, one question is how do I use CMemMapCppClientDlg dlg;
elsewhere in the MemMapCppClient.cpp while still retaining the apps compilability?
Do the same thing that you have done earlier with the CString m_strContent by declaring the CMemMapCppClientDlg on the MemMapCppClient.h. Try if that works first.
e40s wrote: The bigger question is this: Even when all of this compiles perfectly, and my program works great--even the MessageBox gets displayed on cue--no content is there in the MessageBox. Never in any incarnation of a successful compile do I get content within the MessageBox. The prog always works perfectly otherwise.
Make it into this: MessageBox(strContent, _T("Like this:"));
By default the message box has an OK button so no need to add MB_OK there.
|
|
|
|
|
That suggestion works in some other parts of the program, but in this, MessageBoxA demands four arguments.
But there is something else I'm noticing: The strContent may never appear in the box because it's never arriving from GetContent().
In MemMapCppClientDlg, I added a MessageBox() to GetContent(), above the return, like so:
MessageBox(_T(m_strContent), _T("Forget this."));<br />
return m_strContent;
and got no content inside the MessageBox's client area. It's possible m_strContent isn't delivering a value.
Can someone please look at this--maybe specifically the header file, and see if something's lacking. A pointer maybe? I don't know; I'm lost.
Thanks again all.
|
|
|
|
|
It will be better if you use the < pre > < /pre > tags to englobe pieces of code, and the < code > < /code > to highlight little things. Is easier to read on the other way. And with pre the horizontal limit for a line is bigger, so commands won't be broken in different lines not so oft.
EDIT: Maybe a Site Admin could change the label from tags.
code -> Highlight (or something like that)
pre -> snippet (or something like that)
I had the same mistake at the beggining, I thought "paste code = code tag"
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
Thanks for this, Nelek. I didn't know about pre.
|
|
|
|
|
Limit your use of the _T() macro to string literals, not variables.
e40s wrote: I get "initialization" errors.
This is too vague. What statement produces this?
e40s wrote: CString strContent = dlg.GetContent();
At this point has CMemMapCppClientDlg::OnDataReady() been called to add content to m_strContent ? If not, MessageBox() will obviously show nothing.
Why not use AfxMessageBox() instead? It won't solve your problem, but it takes much less code.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thanks for replying David.
>> Limit your use of the _T() macro to string literals, not variables.
Thanks for the education. Will do.
>> This is too vague. What statement produces this?
My fault for bringing that initialization stuff up. I was straddling the statement in select case areas where I shouldn't have been. That problem's been solved, I believe, and pales in comparison to this:
>> At this point has CMemMapCppClientDlg::OnDataReady() been called to add content to m_strContent? If not, MessageBox() will obviously show nothing.
Yes. And that's the mystery.
But now I've confirmed, as I've written this morning, that even a MessageBox() statement inside of GetContent() fails to reveal the value of m_strContent. What's up with that?
(See, the thing is, if I take the comments off of the message box inside ::OnDataReady, the value appears just fine over there.)
Something missing from the header file? From the source? I wrote it up wrong from BK's script? I dunno.
>> Why not use AfxMessageBox() instead? It won't solve your problem, but it takes much less code.
Okay. I didn't know about that one.
-- modified at 11:18 Friday 9th November, 2007
|
|
|
|
|
e40s wrote: Yes. And that's the mystery.
But according to the code snippet you've shown, OnDataReady() has not been called prior to calling GetContent() . You are constructing a CMemMapCppClientDlg object and immediately calling GetContent() which will obviously return an empty string.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
This is my observation.
My Excel file loads this DLL (the whole thing you're looking at in code). It's a DLL because I transmogrified the CodeProject sample from an .exe into one. Because it was originally authored as a Dlg, that window aspect of it remains. Anyway, when the Excel file loads this DLL, said Dialog immediately gets displayed on the screen (it's small and mostly blank, with a simple label in the center). Its job is to pass messages--because only a window can do that, evidently. When I'm finished with it, I destroy the window with a PostMessage().
Anyway, nothing happens display-wise--besides that dialog sitting there--until I fire up the Java side of this, which dispatches a message, via JNI, that is received by this DLL precisely at ::OnDataReady. Whatever content that JNI has passed immediately lands in m_strContent, and lights up the uncommented MessageBox.
To my knowledge, the Excel file way over at the other side does not touch OnDataReady. (Or I hope not. I don't want it to.) Excel is playing only at the CMemMapCppClientApp side--not the CMemMapCppClientDlg side.
Excel, and its loading of the DLL, never touch OnDataReady. (If they did, I would have seen the uncommented MessageBox inside OnDataReady light up immediately upon such an event. I never do.)
It is later, using Excel after Excel has already loaded the DLL prior, I only attempt to retrieve m_strContent value after JNI has already infilled it on the OnDataReady side. [The motivation for yesterday's and today's threads.] If I hear you right, you're suggesting that my invocation of the DLL at any time from Excel has an impact on OnDataReady. Because I never see any evidence of that--i.e., the uncommented MessageBox inside OnDataReady--I don't believe that suggested sequence of events to be the case.
I am calling CMemMapCppClientDlg::GetContent() exclusively from Excel by way of CMemMapCppClientApp. That action per se never, and shouldn't if I'm not mistaken (correct me if I'm wrong), in any way have an impact on OnDataReady. It can't.
-- modified at 12:25 Friday 9th November, 2007
|
|
|
|
|
All this extra stuff aside, the fact still remains that:
CMemMapCppClientDlg dlg;
CString strContent = dlg.GetContent();
MessageBox(NULL, strContent, _T("Like this:"), MB_OK); will do nothing but display an empty message box. You can verify this by initializing m_strContent to something in CMemMapCppClientDlg 's constructor.
After looking at what you've provided, it seems you have two instances of CMemMapCppClientDlg : the main one instantiated from CMemMapCppClientApp::InitInstance() , and another one instantiated from SumOneToArray() . Is that correct?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
>>After looking at what you've provided, it seems you have two instances of CMemMapCppClientDlg: the main one instantiated from CMemMapCppClientApp::InitInstance(), and another one instantiated from SumOneToArray(). Is that correct?
I don't believe so, but I can't say for certain. Can you tell me? Here's the full MemMapCppClient.cpp (ole2.h doesn't show up in our display here for the second #include because it's enclosed in less than/greater than punctuations):
#include "stdafx.h"
#include <ole2.h>
#include "MemMapCppClient.h"
#include "MemMapCppClientDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
VARIANT _stdcall CheckExcelArray(VARIANT& ExcelArray)
{
VARIANT dvout;
switch(ExcelArray.vt)
{
case VT_DISPATCH:
{
EXCEPINFO excep;
DISPPARAMS dispparams;
unsigned int uiArgErr;
DISPID dispidValue;
LPOLESTR XName = L"Value";
ExcelArray.pdispVal->GetIDsOfNames(IID_NULL, &XName,
1, LOCALE_SYSTEM_DEFAULT, &dispidValue);
dispparams.cArgs = 0;
dispparams.cNamedArgs = 0;
ExcelArray.pdispVal->Invoke(dispidValue, IID_NULL,
LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET,
&dispparams, &dvout, &excep, &uiArgErr);
ExcelArray.pdispVal->Release();
return dvout;
}
break;
default:
return ExcelArray;
break;
}
VariantClear(&dvout);
VariantClear(&ExcelArray);
}
VARIANT _stdcall SumOneToArray(VARIANT sourceArray)
{
CMemMapCppClientDlg dlg;
CString strContent = dlg.GetContent();
if(sourceArray.vt==VT_DISPATCH)
sourceArray = CheckExcelArray(sourceArray);
long ncols, nrows, i, j;
ncols=(sourceArray.parray)->rgsabound[0].cElements;
nrows=(sourceArray.parray)->rgsabound[1].cElements;
VARIANT *dArray = new VARIANT [nrows*ncols];
for(i=0; i<nrows; i++)
{
for(j=0; j<ncols; j++)
{
long indi[] = {i+1,j+1};
SafeArrayGetElement(sourceArray.parray, indi, &dArray[(i*ncols)+j]);
}
}
VARIANT destArray;
destArray.vt = VT_ARRAY | VT_VARIANT;
SAFEARRAYBOUND sab[2];
sab[0].lLbound = 1; sab[0].cElements = nrows;
sab[1].lLbound = 1; sab[1].cElements = ncols;
destArray.parray = SafeArrayCreate(VT_VARIANT, 2, sab);
for(i=0; i<nrows; i++)
{
for(j=0; j<ncols; j++)
{
long indi[] = {i+1,j+1};
switch(dArray[(i*ncols)+j].vt)
{
case VT_I2:
dArray[(i*ncols)+j].iVal=dArray[(i*ncols)+j].iVal + 1;
break;
case VT_R8:
dArray[(i*ncols)+j].dblVal=dArray[(i*ncols)+j].dblVal + 1;
break;
default:
break;
}
SafeArrayPutElement(destArray.parray, indi, &dArray[(i*ncols)+j]);
}
}
delete [] dArray;
return destArray;
VariantClear(&sourceArray);
VariantClear(&destArray);
}
BEGIN_MESSAGE_MAP(CMemMapCppClientApp, CWinApp)
END_MESSAGE_MAP()
CMemMapCppClientApp::CMemMapCppClientApp()
{
}
CMemMapCppClientApp theApp;
BOOL CMemMapCppClientApp::InitInstance()
{
CMemMapCppClientDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
return FALSE;
}
Here's the full MemMapCppClientDlg.cpp:
#include "stdafx.h"
#include "MemMapCppClient.h"
#include "MemMapCppClientDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
UINT CMemMapCppClientDlg::UWM_DATA_READY = RegisterWindowMessage(UWM_DATA_READY_MSG);
CMemMapCppClientDlg::CMemMapCppClientDlg(CWnd* pParent )
: CDialog(CMemMapCppClientDlg::IDD, pParent)
{
m_pszMemMapFileName = _T("Mem_Map_File-{70122C30-0239-4f98-9D21-36885C8A8121}");
}
BEGIN_MESSAGE_MAP(CMemMapCppClientDlg, CDialog)
ON_REGISTERED_MESSAGE(UWM_DATA_READY, OnDataReady)
END_MESSAGE_MAP()
BOOL CMemMapCppClientDlg::OnInitDialog()
{
CDialog::OnInitDialog();
return TRUE;
}
LRESULT CMemMapCppClientDlg::OnDataReady(WPARAM, LPARAM)
{
HANDLE hMapFile = NULL;
PVOID pView = NULL;
hMapFile = OpenFileMapping(FILE_MAP_READ, FALSE, m_pszMemMapFileName);
if(hMapFile == NULL) {
MessageBox("Can not open file mapping");
return 0;
}
pView = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
if(pView == NULL) {
MessageBox("Can map view of file");
CloseHandle(hMapFile);
return 0;
}
LPSTR szContent = reinterpret_cast<LPSTR>(pView);
int nLen = strlen(szContent);
CString m_strContent;
while(nLen > 0) {
m_strContent += *szContent++;
--nLen;
}
m_strContent += '\0';
m_strContent.Replace("\n", "\r\n");
MessageBox(m_strContent);
if(pView) UnmapViewOfFile(pView);
if(hMapFile) CloseHandle(hMapFile);
return 0;
}
CString CMemMapCppClientDlg::GetContent()
{
MessageBox(m_strContent, _T("Forget this."));
return m_strContent;
}
The full MemMapCppClient.h
#if !defined(AFX_MEMMAPCPPCLIENT_H__6FC73066_4FE5_46F4_891C_2434753F81AA__INCLUDED_)
#define AFX_MEMMAPCPPCLIENT_H__6FC73066_4FE5_46F4_891C_2434753F81AA__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif
#include "resource.h"
class CMemMapCppClientApp : public CWinApp
{
public:
CMemMapCppClientApp();
public:
virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};
#endif // !defined(AFX_MEMMAPCPPCLIENT_H__6FC73066_4FE5_46F4_891C_2434753F81AA__INCLUDED_)
The full MemMapCppClientDlg.h:
#if !defined(AFX_MEMMAPCPPCLIENTDLG_H__A51F6AF7_F28D_461D_8FB3_EFC7B929D99C__INCLUDED_)
#define AFX_MEMMAPCPPCLIENTDLG_H__A51F6AF7_F28D_461D_8FB3_EFC7B929D99C__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CMemMapCppClientDlg : public CDialog
{
public:
static UINT UWM_DATA_READY;
CMemMapCppClientDlg(CWnd* pParent = NULL);
CString GetContent();
enum { IDD = IDD_MEMMAPCPPCLIENT_DIALOG };
protected:
protected:
virtual BOOL OnInitDialog();
afx_msg LRESULT OnDataReady(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP()
CString m_strContent;
private:
LPCTSTR m_pszMemMapFileName;
};
#define UWM_DATA_READY_MSG _T("UWM_DATA_READY_MSG-{7FDB2CB4-5510-4d30-99A9-CD7752E0D680}")
#endif // !defined(AFX_MEMMAPCPPCLIENTDLG_H__A51F6AF7_F28D_461D_8FB3_EFC7B929D99C__INCLUDED_)
-- modified at 12:41 Friday 9th November, 2007
|
|
|
|
|
e40s wrote: I don't believe so...
I do. Observe:
VARIANT _stdcall SumOneToArray(VARIANT sourceArray)
{
CMemMapCppClientDlg dlg;
CString strContent = dlg.GetContent();
...
}
BOOL CMemMapCppClientApp::InitInstance()
{
CMemMapCppClientDlg dlg;
m_pMainWnd = &dlg;
dlg.DoModal();
...
}
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Whoa! How'd you do that?
I'll pull it and see what happens.
|
|
|
|
|
So how do I get around this?
|
|
|
|
|
One way is to make the member variable static . This means that GetContent() will also need to be static .
Your next question (regarding scope) can be answered here.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Okay, I changed it to this:
#if !defined(AFX_MEMMAPCPPCLIENTDLG_H__A51F6AF7_F28D_461D_8FB3_EFC7B929D99C__INCLUDED_)
#define AFX_MEMMAPCPPCLIENTDLG_H__A51F6AF7_F28D_461D_8FB3_EFC7B929D99C__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CMemMapCppClientDlg : public CDialog
{
public:
static UINT UWM_DATA_READY;
CMemMapCppClientDlg(CWnd* pParent = NULL);
static CString GetContent();
enum { IDD = IDD_MEMMAPCPPCLIENT_DIALOG };
protected:
protected:
virtual BOOL OnInitDialog();
afx_msg LRESULT OnDataReady(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP()
static CString m_strContent;
private:
LPCTSTR m_pszMemMapFileName;
};
#define UWM_DATA_READY_MSG _T("UWM_DATA_READY_MSG-{7FDB2CB4-5510-4d30-99A9-CD7752E0D680}")
#endif // !defined(AFX_MEMMAPCPPCLIENTDLG_H__A51F6AF7_F28D_461D_8FB3_EFC7B929D99C__INCLUDED_)
Fascinating. (I mean that sincerely.) Well, the first thing it did was change the argument disposition of MessageBox inside of GetContent(), so I commented that MessageBox line out there.
Attempted a recompile.
The errors:
1>MemMapCppClientDlg.obj : error LNK2001: unresolved external symbol "protected: static class ATL::CStringT<char,class StrTraitMFC_DLL<char,class ATL::ChTraitsCRT<char> > > CMemMapCppClientDlg::m_strContent" (?m_strContent@CMemMapCppClientDlg@@1V?$CStringT@DV?$StrTraitMFC_DLL@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@A)
1>c:\experimental sample\Debug\MFC_DLL_03.dll : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\experimental sample\MFC_DLL_03\Debug\BuildLog.htm"
-- modified at 13:12 Friday 9th November, 2007
|
|
|
|
|
You have not initialized m_strContent . Static data members of classes must be initialized at file scope.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
The link was informative. (For the future, especially about their inability to access non-static class member data using the member-selection operators mentioned. Learning more every day.)
What I tried:
#if !defined(AFX_MEMMAPCPPCLIENTDLG_H__A51F6AF7_F28D_461D_8FB3_EFC7B929D99C__INCLUDED_)
#define AFX_MEMMAPCPPCLIENTDLG_H__A51F6AF7_F28D_461D_8FB3_EFC7B929D99C__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CMemMapCppClientDlg : public CDialog
{
public:
static UINT UWM_DATA_READY;
CMemMapCppClientDlg(CWnd* pParent = NULL);
static CString CMemMapCppClientDlg::GetContent();
enum { IDD = IDD_MEMMAPCPPCLIENT_DIALOG };
protected:
protected:
virtual BOOL OnInitDialog();
afx_msg LRESULT OnDataReady(WPARAM wParam, LPARAM lParam);
DECLARE_MESSAGE_MAP()
static CString CMemMapCppClientDlg::m_strContent;
private:
LPCTSTR m_pszMemMapFileName;
};
#define UWM_DATA_READY_MSG _T("UWM_DATA_READY_MSG-{7FDB2CB4-5510-4d30-99A9-CD7752E0D680}")
#endif // !defined(AFX_MEMMAPCPPCLIENTDLG_H__A51F6AF7_F28D_461D_8FB3_EFC7B929D99C__INCLUDED_)
Darn. Same errors. (Afterward I even tried adding scope resolution to some of the statements on the .cpp. Nothing changed for the better so I took those off.)
|
|
|
|
|