#### Archived

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

# VB programmer, struggling with Cpp

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

## 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 on other sites

http://www.scit.wlv.ac.uk/cbook/chap11.extern.keyword.html

The extern keyword.

Edit:

And this:

http://msdn.microsoft.com/library/en-us/vccore98/html/_langref_extern.asp

aut viam inveniam aut faciam

MoonStar Projects

[edited by - Ronin Magus on January 31, 2003 2:09:17 PM]

##### Share on other sites
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 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)

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 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 DeclarationDim foo As Integer''Subs and FunctionsPrivate Sub Form_Load()...Private Sub bla bla bla

Now, you have this in form2.frm:''Subs and FunctionsPrivate 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 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 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.cppint foo;void foobar(int a);// file2.cpp// make foo & foobar avalible in file2.cppextern int foo;extern void foobar(int a);// now I can use foo & foobarvoid main(...){   foo = 23;   foobar( foo );}

##### 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 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.

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 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 on other sites
ok, that might explain it, ive got a copy of Vis C++ on my bosses computer, ill jump on here as soon as he leaves and test it out. ive been putting both my protoyes and implementations in the header files, i have a nasty feeling that that is a portion of my problem, anyway, thank you and ill let ya know when i get it sorted out.

edit ** ok, i just built a cpp file and a header file, declared my header file and all of the variables where available, weird.
Maybe it has something to do with the fact that the D3DDEVICE is an object as opposed to a first level type. i dont know guys, anyway thanks for your help.

##### Share on other sites
//globals.hextern int g_CrudeAsHell;//globals.cpp#include "glboals.h"int g_CrudeAsHell;//any_other.cpp#include "globals.h"void do_stuff(){if(g_CrudeAsHell==0)   {   //stuff   g_CrudeAsHell=1;   }}

You declare the variable to be ''extern''al in the header that all the cpp files include. In one (and exactly one) cpp file, you need to actually set aside space for the variable (I do this in the global.cpp file.) Then it''s the linker''s job to hook all the references to g_CrudeAsHell together.

You can precompile global.cpp into a static library, then link to that library instead of compiling global.cpp every time. Not a big deal for a couple of globals, but useful for something as large as, say, DirectX.

##### Share on other sites
@ Magmai Kai Holmlor and all, thanks for the help, will advise.