| Assassinbeast (28) | |||||||
|
Hello folks :) I'm reading the book "Programming principles and practice using c++" of Bjarne Stroustrup. At page 293 and exercise(drill) 1... i cant make it to work :/ Its very simple though... there are 3 files: use.cpp, my.cpp and my.h use.cpp has the main funciton. my.h has some declerations. my.cpp has the definition of my.h. heres the simple code:
It gives me a linker error, but i have done exactly as the exercise descripted. :S | |||||||
|
|
|||||||
| Darkmaster (494) | |
|
whats the linker error? also i doubt thats the only error | |
|
Last edited on
|
|
| Assassinbeast (28) | |
|
1>------ Build started: Project: drill chapter 8, Configuration: Debug Win32 ------ 1> use.cpp 1>my.obj : error LNK2001: unresolved external symbol "int foo" (?foo@@3HA) 1>C:\Users\Danny Chu\Documents\Visual Studio 2010\Projects\c++\drill chapter 8\Debug\drill chapter 8.exe : fatal error LNK1120: 1 unresolved externals ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== | |
|
|
|
| nathan10 (122) | |
|
I have the same book. While I did not do that particular exercise, I can see an important error. Stroustrup says that "Note that use.cpp does NOT #include std_lib_facilities.h ..."But your use.cpp above does have the lines #include <iostream> and using namespace std. I suspect that if you delete those 2 lines, your program will compile. I hope someone else explains it better, but I think that you include the input/output library only once in a program. You have done that in the my.cpp file, so it is not necessary to do so again in the use.cpp file. | |
|
|
|
| Assassinbeast (28) | |
|
no no... i did exactly as he descriped, i just didn't write #include std_lib.... in the forum because it would confuse other people since they dont know whats in that folder. But in this case, only cout and cin is used... so i can just include iostream and i dont need std_lib_facilities.h | |
|
Last edited on
|
|
| Zereo (428) | |||||||
extern int foo; is only declaration of variable foo It is not a definition of the corresponding object. To make a definition of the variable you should either initialize it in the same statement as for exampleextern int foo = 0;Or include another statement which defines the object extern int foo;
int foo;So something like this. use.cpp
my.cpp
and my.h
Also use for global variables can be very dangerous. So think about if you really need a global variable when you use one. | |||||||
|
Last edited on
|
|||||||
| Assassinbeast (28) | |
| ahh... thanks alot | |
|
|
|