Problem with an easy exercise :S

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//use.cpp

#include "my.h"
#include <iostream>
using namespace std;

int main()
{
	foo = 7;
	print_foo();
	print(99);


	char f; cin >> f; //keep window open
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//my.cpp

#include "my.h"
#include <iostream>
using namespace std;

void print_foo()
{
	cout << foo;
}

void print(int i)
{
	cout << i;
}


1
2
3
4
5
//my.h

extern int foo;
void print_foo();
void print(int);


It gives me a linker error, but i have done exactly as the exercise descripted. :S
whats the linker error?

also i doubt thats the only error
Last edited on
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 ==========
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.
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
closed account (3qX21hU5)
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 example

extern int foo = 0;

Or include another statement which defines the object

extern int foo; int foo;

So something like this.

use.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "my.h"

int foo;

int main()
{
	foo = 7;
        print_foo();
        print(99);

	char f; cin >> f; //keep window open
	return 0;
}


my.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "my.h"

using namespace std;

void print_foo()
{
	cout << foo;
}

void print(int i)
{
	cout << i;
}


and my.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef MY_H_INCLUDED
#define MY_H_INCLUDED

#include <iostream>
using namespace std;

extern int foo;

void print_foo();
void print(int);


#endif // MY_H_INCLUDED 




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
ahh... thanks alot
Topic archived. No new replies allowed.