I doing the first drill for chapter 8 in Programming: Principles and Practice Using C++.
The drill is worded as such;
"Create three files: my.h, my.cpp, and use.cpp. The header file my.h contains
extern int foo;
void print_foo();
void print(int);
The source code file my.cpp #include my.h and std_lib_facilities.h, defines print_foo() to print the value of foo using cout, and print(int i) to print the value of i using cout.
The source code file use.cpp #include my.h, defines main() to set the value of foo to 7 and print it using print_foo(), and to print the value of 99 using print(). Note that use.cpp does not #include std_lib_facilities.h as it doesn't directly use any of those facilities.
Get these files compiled and run. On Windows, you need to have both use.cpp and my.cpp in a project and use {char cc; cin>>cc;} in use.cpp to be able to see your output. Hint: You need to #include <iostream> to use cin."
I've followed the instructions as I understood them but they seem a bit too ambiguous. My code is the following;
1 2 3 4 5
|
//my.h
//
extern int foo;
void print_foo();
void print(int);
| |
1 2 3 4 5 6 7 8 9
|
//my.cpp
//
#include "stdafx.h" //Visual Studio requires this
#include "my.h"
#include "std_lib_facilities.h"
void print_foo() { cout << foo << endl; }
void print(int i) { cout << i << endl; }
| |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
//use.cpp
//
#include "stdafx.h" //Visual Studio requires this
#include "my.h"
#include <iostream>
int main()
{
foo = 7;
print_foo();
print(99);
char cc; std::cin >> cc; //This just keeps the window open
return 0;
}
| |
I get the following compile errors;
LNK2001 unresolved external symbol "int foo" (?foo@@3HA) File: my.obj Line: 1
LNK2001 unresolved external symbol "int foo" (?foo@@3HA) File: use.obj Line: 1
LNK1120 1 unresolved externals File: LearningProgramming.exe Line: 1
"LearningProgramming" is the project name which becomes the name of the .exe
IAW the content of the chapter, this looks like a drill to practice implementing functions. The two main ways was pass-by-value and pass-by-reference. "print(99)" looks like the pass-by-value so I'm assuming the "print_foo()" is suppose to be pass_by_reference. Something like; void print_foo(int& foo){...}
Any help would be greatly appreciated.