Storing Variables in RUNDATA
Ever since I started writing extensions, I've always stored variables by declaring them at the top of Main.cpp. This has always sufficed my needs, but from what I hear it seems that doing this is considered unethical or sloppy. It also has the disadvantage of not being universal to the extension, a characteristic which I need for my current project.
So I was wondering how to store variables in the RUNDATA structure (if that's what it's called). I can store integers in it with no imminent trouble, and probably some other datatypes. But a lot of times when I try to store something, I get errors asking for semicolons and type specifiers. I, of course, have these, which is what confuses me.
Specifically, what I'm trying to do is store a vector of structures. But when I try to store a vector of any type, I get the errors I mentioned above. I also find that I don't know where to define the structures I need for the vector.
So the question is: How do I store most variables in RUNDATA (specifically vectors), and where can I define a structure that the vector will store?
Any help would be much appreciated. :)
Thanks,
Jaffob
Re: Storing Variables in RUNDATA
Store a pointer to it and (de)allocate it in Runtime.cpp, done ;)
Re: Storing Variables in RUNDATA
You're wanting something like this:
Code:
struct VectorStoresThis
{
INT crazycrap;
};
#include <vector>
typedef struct tagRDATA
{
vector<VectorStoresThis>* pVector;
} RUNDATA;
Then in Runtime.cpp:
Code:
short WINAPI DLLExport CreateRunObject(LPRDATA rdPtr, LPEDATA edPtr, fpcob cobPtr)
{
rdPtr->pVector = new vector<VectorStoresThis>;
// No errors
return 0;
}
short WINAPI DLLExport DestroyRunObject(LPRDATA rdPtr, long fast)
{
delete rdPtr->pVector;
// No errors
return 0;
}
Re: Storing Variables in RUNDATA
That works for storing variables and accessing them, although defining a structure doesn't seem to work. Using Dynasoft's code, I get the same errors as I was getting before — but now for individual elements within the structures. I have them defined at the top of Data.h, right below the following code:
Code:
class rRundata;
typedef rRundata * LPRRDATA;
Any idea how to fix this?
Thanks again. :)
Re: Storing Variables in RUNDATA
It would be very helpful if you would post the exact error you're getting, and the exact line of code (preferably, block of code) that is producing that error.
Re: Storing Variables in RUNDATA
Well I've gotten rid of a few of the errors, but I still get a fair number of other ones. Here's the top part of my Data.h:
Code:
class rRundata;
typedef rRundata * LPRRDATA;
struct MYSUBMENUOBJ {
char * name;
HMENU handle;
} ;
struct MYMENUOBJ {
char * name;
HMENU handle;
vector <MYSUBMENUOBJ> submenus;
RECT excrect;
} ;
#include <vector>
// --------------------------------
// RUNNING OBJECT DATA STRUCTURE
// --------------------------------
// If you want to store anything between actions/conditions/expressions
// you should store it here
typedef struct tagRDATA
{
#include "MagicRDATA.h"
vector <MYMENUOBJ> * menuList;
unsigned int lastError = 0;
} RUNDATA;
typedef RUNDATA * LPRDATA;
The following errors occur on the line where the "submenus" vector is declared in MYMENUOBJ:
Code:
error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'
I guess it's also a vector, so it has the same problem as vectors in RUNDATA do. I also just realized that the errors with declaring variables were not in fact solved. I get the following errors on the vector in RUNDATA:
Code:
error C2143: syntax error : missing ';' before '*'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
And this for the integer:
Code:
error C2864: 'tagRDATA::lastError' : only static const integral data members can be initialized within a class
Thanks again. :)
Re: Storing Variables in RUNDATA
It has to be std::vector... unless you specify "using namespace std;" after including the header.
Plus, you can't define variables in struct definitions! You should do it in CreateRunObject. rdPtr->lastError = 0;
Re: Storing Variables in RUNDATA
#include <vector> must come before you first use the vector. Put it above the second struct definition.
Re: Storing Variables in RUNDATA
Okay, last night I finally got it working. It took me a little while to realize that I would need to use (*rdPtr->menuList) to actually do stuff with the vector (first I forgot it was a pointer :blush:, and then I realized it needed parentheses as well). But it all compiles flawlessly now. :grin:
Many thanks to the three of you. :)
Re: Storing Variables in RUNDATA
You actually don't, unless you want to use the [] operator.
You can still use rdPtr->menuList->at(X), ->insert(...) etc.
Re: Storing Variables in RUNDATA
You're better off making your own runtime structure and storing a pointer to that in rdPtr. Then you don't need to use pointers for everything.
Re: Storing Variables in RUNDATA
Thanks Looki, that makes things easier. :)
Jamie, I'm not completely sure what you mean. Runtime structure, meaning my own version of what the RUNDATA structure does? How would I apply what you are saying to my code?
Re: Storing Variables in RUNDATA
Your own structure for the things you'd normally put in rdPtr, which you store a pointer to in rdPtr and initialise/free in CreateRunObject/DestroyRunObject. Then the constructors/destructors for everything in that structure will be called as normal.
Re: Storing Variables in RUNDATA
So then would I use all the variables in my structure using something like rdPtr->myStructure->myVariable?
Re: Storing Variables in RUNDATA
rdPtr->myStructure.myVariable, actually, unless myVariable specifically needs to be allocated dynamically (e.g. an array or a buffer).
The reason you can't just stick a std::vector into RUNDATA is because it's a C struct and would never call std::vector's constructor (and so if you wanted it in RUNDATA, you'd have to store a pointer instead and construct it yourself). But if you have your own C++ struct, then you could store a std::vector directly in it and it would be properly constructed and destructed.
Re: Storing Variables in RUNDATA
Quote:
Originally Posted by Retriever2
rdPtr->myStructure.myVariable, actually, unless myVariable specifically needs to be allocated dynamically (e.g. an array or a buffer).
It'd still be myStructure->myVariable if myStructure was a pointer.
Re: Storing Variables in RUNDATA
Yes, you're correct, I guess my brain is turned off today.
If myVariable was itself a complex data type then what I said would be correct, referring to accessing members of myVariable.
Re: Storing Variables in RUNDATA
I think I'll just keep it the way I have it, since it's already working fine. The pointers aren't being any trouble.
What exactly does -> mean? I figured out C++ by knowing Java, but in Java all the fancy memory stuff is done automatically. So I can sometimes be baffled when it comes to pointers and memory. :(
Re: Storing Variables in RUNDATA
There's not a big difference to . really, -> is used when you point to a struct and . is used when you directly access it, e.g.
PHP Code:
struct MyStruct
{
int Variable;
};
int main()
{
MyStruct foo;
foo.Variable = 10;
// Create a pointer to foo
MyStruct* pointer = &foo;
pointer->Variable = 10;
}