views:

272

answers:

4

noob question right here. How do you pass values between 2 different cpp files in the same project? Do you make objects? if yes, how does the other cpp file see it? some enlightment pls..

EDIT: some clarifications. I'm trying to interface direct input with a program (of which I have the plugins sdk). I'm trying to interface a joystick with it. It seems that there is no main function when I look through the code, but I might be wrong (like, I might not look in the right files). I know programming, and pointers and stuff, classes. Is there anything I should learn or get into in order to achieve what I want?

A: 

Normally you define a function in one cpp file, then declare that function as extern in a header, and include the header in whatever other cpp file needs to use the function. You can then write code that calls the function normally. At link time you need to supply the object files that resulted from both cpp files, and the linker ...links them together, so the function call in one file passes the value correctly as you call the function that was defined in the other cpp file.

Jerry Coffin
Note that `extern` is implicit for function declarations. While you can write `extern void f()`, nobody ever does it and so it might confuse readers.
sbi
A: 

Referencing code in a different file typically makes use of #include

McAden
This is how you typically do this, but there are other ways...
John Dibling
qualified the statement.
McAden
+1  A: 

You could declare a global variable in a header file like so:

extern int GlobalVar;

And in exactly one compilation-unit (cpp-file) you have to initialize it:

int GlobalVar = 5;

Any other compilation unit that includes the header now shares the same instance of the global variable (I hope that syntax is correct, i rarely use it).

One should mention, that your question indicates a general lack of understanding of how programs in C++ should be organized. In short, you usually have a file called main.cpp that contains the entry-point of your program. The rest, in C++, is done in classes most of the time. A class is usually split into two parts, the declaration of the class in a header file, and the implementation of the class in a cpp file. To use a class, you include the corresponding header file. This is the short version, and there is much more to tell, but this should give you a good starting point.

Space_C0wb0y
hmm, in the file in which I use GlobalVar (not the file in whch I set it), it gives me an undeclared identifier error. see my edit
jello
Are you sure that you have included the header?
Space_C0wb0y
+2  A: 

In all but few cases it's a bad idea to share data among compilation units. A compilation unit, just to get you up to speed with the C++ terminology, usually effectively refers to an implementation file (with extension .cpp, or .cc etc.). The way we have the various compilation units "communicate" with each other is with header files and functions, rather than raw data.

Suppose we have an implementation file main.cc and a second implementation file human.cc. We want main.cc to communicate with human.cc. Here we go:

// main.cc
#include "human.hh"
int main()
{
    make_the_human_dance(60);
    return 0;
}


// human.hh
void make_the_human_dance(int duration);


// human.cc
#include "human.hh"
void make_the_human_dance(int duration)
{
    // define how a human dances properly
    // ...
}

Under the same principle you can use classes for communication. Declare the class in the header file and define the class' methods in the implementation file. Sometimes you must provide the implementation of functions in the header files, but that is already going offtopic.

wilhelmtell
+1 because examples are really the best way to express ideas. I was too lazy for one.
Space_C0wb0y
I think I know what classes, headers and implementations are. But I thought i was missing something. like, something I didn't know....so by your answer, that means that you can't pass values to implementation files without a main?how about when you write plugins? or dlls?
jello
@jello No, we need a `main()` function if we are to build the program as an executable. Because `main()` is the program's point of entry, the first thing the operating system will look for when we run the built executable. Now, if you really want to share data between any two compilation units then you can most certainly do that. What you do is declare a global variable, and then declare it again with the `extern` keyword in any other compilation unit where you want to use that global variable. I can extend my answer to cover this if you wish, but this technique is usually something to avoid.
wilhelmtell
@jello Also, there's _always_ something we don't know. This is inherent to C++. ;)
wilhelmtell
@wilhelmtell thx for the extern. you too SpaceCowboy. i'll try it tomorrow. but when you develop plugins, it's dlls right? not executables?
jello
@wilhelmtell re `extern`: You _define_ the variable in one translation unit and _declare_ it (preferably by including the declaration) in the ones that want to access it. See http://stackoverflow.com/questions/1410563/1410632#1410632.
sbi