|
The easiest way to do it would be to put a function in your dialog class that takes a doc or view pointer, example:
void COrigin::SetDocument(CMyDocument *pDoc)
{
myDocPtr = pDoc;
}
And then wherever you need to use the document, you can use myDocPtr. Whoever creates and calls up the dialog would be responsible for setting the document pointer, it'd go something like this:
COrigin dlg;
dlg.StDocument(blah);
dlg.DoModal();
If you have an SDI doc, you could probably fidn a way of geting the document from the applicaiton class, but doing it tihs way makes it more flexible in case you ever go MDI.
"When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity." - Albert Einstein
|
|
|
|
|
I'm working on a project and i need some sort of list object in memory, it just has to exist in memory, i'm not talking about a gui list or anything. The list should be able to hold anything i want... like.. every entry should have its own unique ID (an integer value), a name (a string value) and possibly others. But each new entry will have the same types of values as the others, if u know what i mean. Then i should be able to add entries and remove them quite easily by calling a function on it or something sort of like this:
list.AddEntry(<unique id>,<name>, etc...);
list.RemoveEntry(<unique id>);
I hope u know what i mean. Now my question is, how is this commonly done? Do people build their own structure or something? and write all the add and remove code themselves? Or is there maybe an existing object i can use (and i'm not talking MFC, just basic c++ with stdlib etc.). I've seen someone use a vector or something before but i'm not sure what thats about, and they used a strange way to declare the variable like..
int<vector> bla;
or something like that..can't quite remember.
But anyways i'm just looking for the right way to do it
Thank you.
Kuniva
--------------------------------------------
|
|
|
|
|
If the IDs are not sequental use a map, otherwise a list or vector will do.
vector<int> intvec;
John
|
|
|
|
|
Thanks! But do you happen to know where i can find more information about a "map" since i have no clue what that is..
Kuniva
--------------------------------------------
|
|
|
|
|
|
hi
I have a single document , MFC project, with a few dialog classes.
In one of the dialog member functions, i have to get access to a member variable of my View class.
How can I do that..
its basically a pointer to the view class
what is the code
Ehsan Behboudi
|
|
|
|
|
Pass the dialog a pointer to the view class before you call DoModal().
John
|
|
|
|
|
how u write a sample code please?
Ehsan Behboudi
|
|
|
|
|
Where do you create the dialog? From your view??
John
|
|
|
|
|
i created the dialog class associated with my menu.
say, click on File, then , Move,, the dialog (COrigin) pops up
now,, in the dialog, when the user clicks on a button, IDC_BUTTON1
the function associated with this button is:
void COrigin::OnButton1()
{
//I have to get a pointer to change a variable in the View or Doc class
//which ever is easier.
}
Ehsan Behboudi
|
|
|
|
|
If the data is needed in your view, you might want to keep the variable as a member of the view, and pass a pointer to the view to the dialog.
or send a message ( with SendMessage ) containing the variable and handle it in the view.
Maximilien Lincourt
"Never underestimate the bandwidth of a station wagon filled with backup tapes." ("Computer Networks" by Andrew S Tannenbaum )
|
|
|
|
|
So the dialog is created in the view or the doc?
I assume it is the view.
Add variable to your dialog
class CMyDialog : public CDialog
{
public:
CMyView* m_pMyView;
}
Then in our view
CMyView::OnMenuButtonPushed()
{
CMyDialog dlg;
dlg.m_pMyView = this;
dlg.DoModal();
}
John
|
|
|
|
|
Getting your data directly from the view in a single document application is bad design to some degree, because it breaks the SDI template. You can expect major reusability issues when you link your dialog directly to the view, since the view in turn is linked to the document.
If you can help it, try declaring member variables for the data in the dialog and fill those members with the actual data from the document just before you call domodal. After the dialog returns, read the changes from the member variables back to the document. This way you’ll get a dialog class, which does not ‘directly’ depend on a particular view or document class.
Concepts like this will not make your app work any different, but they will help maintenance and improve reusability.
Lorenz Prem
Microsoft Corporation
|
|
|
|
|
I'm having a hard time getting the rand function to work. I know that rand ( ) retruns a randow number, but I'm not sure how to do it? I posted some simple code below. What should be my return? Do I just pass the highest number of my range into the argument box of rand ( )? I'm looking through my books, but have found little to help me. How do I get rand( ) to work? Also, what's the difference between rand, srand and nrand. I know that srand starts a new seed sequence but am not sure what that means.
<br />
#include <stdlib.h><br />
#include <iostream.h><br />
<br />
int main()<br />
{<br />
int x = 150;<br />
int rand (x);<br />
return ;<br />
}<br />
Thanks, Dave
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
|
|
|
|
|
If you don't use srand(TIME(NULL)) to seed rand(), you'll get the same random numbers every time!
if the maximum should be 150 you can do the following:
int nRandomNumber = (rand() % 150) + 1;
your code will produce linker errors!
int rand() is a declaration. the rand function does not take parameters and your main function does not return a value!
hope this helps...
A student knows little about a lot.
A professor knows a lot about little.
I know everything about nothing.
|
|
|
|
|
Bob, I'm a bit confused on the rand function being a declaration. Does this mean that rand ( ) should only appear in a header file and not in the source file. How can you tell when a function is a declaration or if it belongs in the source file? Is it because of the "int" in front of rand( ) that makes it a declaration?
Thanks, Dave
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
|
|
|
|
|
DaveE9th wrote:
Does this mean that rand ( ) should only appear in a header file and not in the source file.
No it does not and most likely it will be in the source file.
John
|
|
|
|
|
A declaration looks like this: return type name(arg list);. It's predefined, that means that you only have to use it! Therefore it should only appear in a source file!
You should seperate 3 things:
1) declaration => Header File
2) Implementation => Source File
3) Call to a function => Source File (within other functions)
A Implementation looks like this:
return type name(arg list)
{
// source code
}
I assume you know how to call a function...
hope this helps
A student knows little about a lot.
A professor knows a lot about little.
I know everything about nothing.
|
|
|
|
|
srand() seeds the pseudo random number generator. Many, if not most, developers just use the time function for this.
rand() returns a pseudo random number from 0 to RAND_MAX. To get a random number in an range, use the modulo operator with the max value plus one. In other words to obtain a random number from 0 through 5 do the following:
int x = rand() % 6;
Never heard of nrand().
|
|
|
|
|
Thanks, I'll try those out.
Dave
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
|
|
|
|
|
The following two code examples do compile, but only yield a message that says to press a key to continue. Shouldn't I see a random number "x" cout to the screen from rand ( ). What am I doing wrong here?
<br />
#include <cstdlib<br />
#include <iostream><br />
<br />
using namespace std;<br />
<br />
void main()<br />
{<br />
int x = (rand() % 150) + 1;<br />
cout<<x<<endl;<br />
} <br />
Here's the other example...
<br />
#include <cstdlib><br />
#include <iostream><br />
<br />
using namespace std;<br />
<br />
void main()<br />
{<br />
int x = rand() % 151;<br />
cout<<x<<endl;<br />
}<br />
Thanks, Dave
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
|
|
|
|
|
They both work fine for me (Win/XP, Vstudio v6).
What compiler/system are you using?
|
|
|
|
|
I can't believe what my problem was. I'm still learning VC++ 6.0. I wanted to try some new code so I just replaced the code on my screen with the new code. I went to compile and evertyhing looked ok, it wasn't. I found something weird (at least I think it is weird) about VC++ 6.0, it doesn't compile what's on your screen unless you save it first. The source cpp file listed in my view folder did NOT change with the new code I typed in. VC++ compiled the cpp file listed in the view folder, not the cpp file I had open in front of me. So the code in front of me (that was all messed up) was seemingly compiling ok, in reality it was the cpp file in my folder that was compiling ok instead.
I did get rand( ) to work finally. I used time(0) as the argument and it works great. Thanks much, Dave
"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson
|
|
|
|
|
Ok, I'm completely baffled. I am using a CFile to store information for my CDocument class in an SDI application and passing the CArchive to the serialize function. (We'll call the filename "datafile.dat") I have had absolutely no problems with my code up until this point. I am simply storing 4 CStrings in the file ("datafile.dat"). These 4 CStrings just hold locations of further files which the application will access. I am running into a problem when I try to open "datafile.dat" for storing...if any of the 4 CStrings is holding the address of a network file (a file on another computer) then "datafile.dat" will not open for storing. If the 4 CStrings hold the paths of local files or pretty much anything else, then "datafile.dat" has no problem and stores the information. Keep in mind, the location of "datafile.dat" is local and application specific...this file is not shared between multiple instances of my application.
I'm including my code snippet where I try to open the CFile for storing. This code works fine for all cases EXCEPT when one of the 4 CString variables (which is all that gets stored) contains the path of a network file instead of a local file. In the case of the network file path, the code gets stuck in the while loop. Oh, one other thing...the file DOES exist, it is created in the constructor if it doesn't currently exist, which by the time this code is reached, the file exists.
/* My Code for opening the file for storing. The FileName variable never changes after being initialized (I've message boxed it, the FileName variable isn't causing the InitFile to not open, it is the correct file at all times). */
CFile InitFile;
while ( !InitFile.Open(FileName, CFile::modeWrite | CFile::shareExclusive) );
CArchive archive(&InitFile, CArchive::store);
Serialize(archive);
archive.Flush( );
archive.Close( );
InitFile.Close( );
Thanks for taking to time to read such a long post, and I hope I was clear in my explanation, and that somebody understands why this file isn't opening!
Douglas A. Wright
dawrigh3@kent.edu
|
|
|
|
|
I don't know the source of your problem, but maybe you can try to use a CStdioFile for it and open int in CFile::typeBinairy mode. Maybe this will work.
Hope this helps...
A student knows little about a lot.
A professor knows a lot about little.
I know everything about nothing.
|
|
|
|