using ints created in one cpp in another

Is there any way of using ints that you have created in one cpp file in another cpp file

Any help is much appreciated

=)
example:
int.cpp
int my_global_int;

main.cpp
1
2
3
4
5
6
extern int my_global_int;

int main(){
    my_global_int = 5;
    return 0;
}
When i try this it works and compiles, but when it gets to linking it throws up:

mainScroller.obj : error LNK2001: unresolved external symbol "int energy" (?energy@@3HA)

any ideas why?
you should add both cpp files into your project if your using an IDE(visual C++, code::blocks, dev-c++, etc)
http://www.cplusplus.com/forum/unices/2313/page1.html#msg8761

Make sure that you are not trying to compile only one obj file into your executable, and that you are using the proper compiler and linker for stuff.

Hope this helps.
hmmm

@blackcoder41- both of the cpp files are included in the project.

@Duoas - All object files seem to be in my project (im kind of a newbie to c++ so it might not be) thanks for the help though =/
i just tried it using visual c++ and codeblocks, and it works..
tried what? the example that hamsterman provided isnt my work (:
Ncotte: You have a problem with which files you're compiling and linking.

What IDE are you using? Visual Studio? Code Blocks?


EDIT:

It's also worth noting that using globals like this is filthy and you really shouldn't be doing this in the first place.... but whatever.
Last edited on
im using visual studio...

well basically my tutors giving us a basic shell of a scroller and we have to create our own classes and put all our variables such as lives, energy, score etc... in them classes then make them work in the cpp he's given us...
If your 'lives' variable is global as it seems to be from your post, then it's not part of a class... so you're doing it wrong.

As for your other problem... are you sure both 'int.cpp' and 'main.cpp' are part of your project? This error could only be caused by them not being linked together, which indicates one of them (whichever one has the 'extern', in this case 'main.cpp') isn't part of your project.
Ahh thanks i shall see if i can find out how to correct that then haha

and also the example provided by hamsterman isnt my work.
could you post the given cpp file?
oh and any helpful links that will help me understand what im doing wrong would be appreciated?


gawd i hate being a newbie xD
not really its over 300 lines long lol
just explain the situation then.. what will the given cpp should do with the other?
ok well basically...

i need to store my lives, energy and score in my own classes (creating my own header file and cpp file with it) and i should be able to use these in the cpp that my tutor gave us which will render them to a display window

//Monitoring the display function
				 std::stringstream str;
				 str<<"Score: "<<timeAccumulator;//displays the score

				 str<<"   Energy: "<<energy;//displays the energy bar
				 str<<"   Lives: "<<lives;//displays the number of lives
				 std::string s;
				 std::getline(str,s);
				 str.clear();				 
				 sf::String TextScore(s.c_str(),sf::Font::GetDefaultFont(),15);
					TextScore.SetPosition(0,100);
					TextScore.SetColor(sf::Color(255, 0, 0));
					App.Draw(TextScore);


thats my problem that i need to use the lives and energy in the above code but in my own cpp.
if you would use the energy and lives as the above code then, it must be global and it doesn't belong to a class or struct..
Topic archived. No new replies allowed.