Help with Drill

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.
foo was declared in global scope and should be initialized in global scope.

Made a working example here https://repl.it/repls/HandmadeSpeedySequences . Files in the project appear on the left.

And no, you don't really need to include stdafx.h; this is for precompiled header savings for huuuuuge header files. Not something you need to worry about right now.
Hello Joe C,

My experience with "extern" is limited, but my understanding is that the use of "extern" is in a different ".cpp" file from where the original was defined. At least that is the way it tends to work for me.

I did try icy1's suggestion of defining "foo" in the global scope of the "use" file and it did work. It compiled with no warnings or errors and displayed 7 and 99.

You and I both know that "stdafx.h" is part of the VS way of setting up the main file and needs to be used in other ".cpp" files, but it is best to leave it out when posting code here because most people can not or do not use it. These days I create an empty project and add the header files I need.

Another hint #include "std_lib_facilities.h" is specific to the book you are using, but contains many header files you may never need. It would be better to learn what header files you need for a program rather than a header file that includes every possible header file.

Hope that helps,

Andy
Thanks for the replies.

The project type was Windows Console which worked up to this point. So I went with a "Blank" project adding the two .cpp files and the .h to the project as needed. This allowed me to remove stdafx.h but creating the files exactly as described in the book still left me with the same three errors.

I want to stay true to the variable being declared in my.h but defined in use.cpp which I believe is part of the point of the drill.

For now I'm going to skip it. There were only 3 drills and I did the other two no problem.
Hello Joe C,

Not having read the book yet I would wonder if part of the drill is to figure out what is missing.

Some day in the future this one of the first books I will have to acquire.

The solution was not hard, refer back to ick1's message, and understand when using "extern" something first that variable has to be defined somewhere. Using the "extern" is like saying here is what the variable looks like, but it is defined in another file. Thinking back I do believe that the original variable needs to be defined in the global scope of the file for "extern" to work,

Hope that helps,

Andy
Topic archived. No new replies allowed.