Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Dreddnafious Maelstrom

VB programmer, struggling with Cpp

This topic is 5678 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok guys, anyone feel like helping out? I''ve written a graphics engine in visual basic, works well, was lots of fun etc... Im now trying to write a graphics engine in Cpp, using VC++. i understand (at least academically) the various constructs of C++. (ie pointers, classes, polymorphism, templates) The problem im having is quite noobish, but im going to ask anyway. my biggest issue with the new language thus far, has been understanding the linking and includes etc.(VB hides most of this from you) Ok, and now the problem. to make this simple, my major issue is how to make a variable global throughout the project. for examplei dont have the SDK here at work so the syntax is obviously going to be jacked) at the top of my main .cpp file i declare IDIRECT3DDEVICE8 g_pD3Ddevice; (im actually using dx9, but cant remember the type name.) So inside this cpp file i can do anything i want with the device and all is well, but in my mesh class (for example) when i try to use my g_pD3Ddevice->Setrenderstate (or whatever) it kicks me the **error, ->setrenderstate must have a class,struct, or union to its left. Basically telling me that it doesnt know what the hell im talking about. It also gives me the undefined type error. Ive experimented with a bunch of different oddball includes and such, ive also looked around in various tutorials(mostly in the SDK) and i can see examples of other files using variables declared in completely different files.(which is what im struggling with) but i cant seem to put 2 and 2 together. If i comment out the class function member that makes reference to the d3ddevice then it compiles just fine, but when i leave it active it starts kicking me into errorsville. Someone mind helping a C++ noob out?

Share this post


Link to post
Share on other sites
Advertisement
I usually Have one header file i.e "headers.h" in that header i define all the other header files like "windows.h" and "d3dx8.h" or what ever. Then i just include that header file in each cpp file which uses any of the headers. This way u dont need to rewrite em all. I think the problem u are including the header in on file but not in the other. This could be your problem. Correct me if iam wrong though


-----
Losec
-----

Share this post


Link to post
Share on other sites
@ Ronin and Losec, hey guys thanks for your help. another quick question. (which you may feel free to ignore, im just stuck at work and cant experiment with it just this second)

Ronin, your second link which points to the msdn says:

The extern keyword declares a variable or function and specifies that it has external linkage (its name is visible from files other than the one in which it's defined). When modifying a variable, extern specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends). The variable or function may be defined in another source file, or later in the same file. In C++, when used with a string, extern specifies that the linkage conventions of another language are being used for the declarator(s).

Declarations of variables and functions at file scope are external by default

it seems to imply that a variable declared at the top of a file(ie file level scope) would automatically be extern'd that hasnt been my experience, am i just reading it wrong?

anyway, thanks for yalls help again.


[edited by - Dreddnafious Maelstrom on January 31, 2003 2:54:03 PM]

Share this post


Link to post
Share on other sites
You are a VB programmer rite? Here:
You know what Class Module, Module, and Form are.
Let''s say, you have this in form1.frm:
''In General Declaration
Dim foo As Integer

''Subs and Functions
Private Sub Form_Load()...
Private Sub bla bla bla


Now, you have this in form2.frm:
''Subs and Functions
Private Sub Form_Load()
{
foo = 10 ''using foo from form1.frm
}


Can you do that? As far as I remember, you can''t, considering using Option Explicit in both form1 and form2. This draws a conclusion that foo belongs to form1 only, and not available in form2. It doesn''t belong to any other classes, modules or forms. Even if you make foo a public variable, you still have to reference the form1.

Form1.foo = 10


The same thing applies in here. If you declare a variable on the top of a file, that variable belongs only to that file. That file is global only to that file, which means that every function in that file still can use it but not the other functions in the other files.

The extern keyword is used to make this variable even more global. Now, all files .cpp files can use and share this variable. It''s like declaring a variable or function in Modules (in VB). Since we don''t have modules in C/C++, we use extern keyword and header files.


return 0;

Share this post


Link to post
Share on other sites
Sorry i cant help you with that .... its just that i thought that the extern KEYWORD had been excluded from C/C++ or wasnt standard yet. Also, have you solved your problem yet?


-----
Losec
-----

Share this post


Link to post
Share on other sites
AFAIK ''extern'' has always been part of the C & C++ standards. I kinda concerned you''ve got the wrong end of the extern stick tho. ''extern'' is used to bring a variable into another file, not to export it (which it sounds like you are thinking). ie


  
// file1.cpp


int foo;

void foobar(int a);


// file2.cpp


// make foo & foobar avalible in file2.cpp

extern int foo;
extern void foobar(int a);

// now I can use foo & foobar


void main(...)
{
foo = 23;
foobar( foo );
}

Share this post


Link to post
Share on other sites
A way to solve this problem is to define
IDIRECT3DDEVICE8 g_pD3Ddevice;
in a header file e.g. "Globals.h". Then include this in the files neccessary. This should solve your problem. Reply if you have any problems


-----
Losec
-----

Share this post


Link to post
Share on other sites
Yeah my trusty "Beginning Visual C++ 6" book, makes only a single reference to the keyword extern, and that relates to extracting variables fom DLL's

@ Losec, OK man, ill try the globals.h and the #include "globals.h" but i thought id already tried that. If i recall correctly i got a D3DDEVICE pure type, can only be used in functions error.

@ NickB, from what i have just read on the MSDN (thanks to Ronin)
that is the right way to go.

@ alnite, you said "The extern keyword is used to make this variable even more global. Now, all files .cpp files can use and share this variable. It's like declaring a variable or function in Modules (in VB). Since we don't have modules in C/C++, we use extern keyword and header files." did you mean only .cpp files? or am i taking that out of context?

Anyway, you guys dont wear yourself out on this, im stuck at work and im just asking questions at the moment without turning around and trying to implement them. So i am currently guilty of the worst kind of noobyism. When i get home tonight ill try to implement yalls help and see how i come out.

That said, if im not testing your patience too much, the next question is "am i going about this the right way?"

The basic problem is i have all of these DX interfaces that im going to need throughout the program. some of them will be encapsulated completely in their own object. (like .x files)
but many of them will be referenced by a bunch of different objects.(D3DDEVICE for sure, as well as a bunch of the DXMusic interfaces) , from what ive read i should be able to declare a bunch of interfaces at file scope level in a header file like "interfaces.h", then in a file that i need to reference an interface i should be able to type:

#include "interfaces.h" and have the variable names be available throughout that file. it simply hasnt been working out that way.
anyway, im interested in seeing if there is a smarter way that i just dont know about, or if im going about it the right way.(the idea at least, certainly not the implementation)

once again, thanks for any time you take on this.


Dreddnafious Maelstrom

"If I have seen further, it was by standing on the shoulders of Giants"

Sir Isaac Newton


[edited by - Dreddnafious Maelstrom on January 31, 2003 4:28:26 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Dreddnafious Maelstrom
@ alnite, you said "The extern keyword is used to make this variable even more global. Now, all files .cpp files can use and share this variable. It's like declaring a variable or function in Modules (in VB). Since we don't have modules in C/C++, we use extern keyword and header files." did you mean only .cpp files? or am i taking that out of context?
Yes, only .cpp files. Basically, when you compile your project, only .cpp files are compiled. The headers, they are ignored, unless you include them in .cpp files. I just tried this in VC++. I add a dummy header file to my project, write this code:

class hello word
{
int foo;
int char super char foox;
};

I didn't include this header file in any of .cpp files. I compiled, I didn't get errors.

quote:

Now, all files .cpp files

I forget to add an important detail here. All .cpp files that include the header files, containing the global variables.

return 0;

[edited by - alnite on January 31, 2003 4:45:16 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!