Header files

Hello,

Getting error when trying to compile these files:

my.h:

1
2
3
 extern int foo;
void print_foo();
void print(int);


my.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include "my.h"
#include <iostream>


using namespace std;

print_foo()

{

 cout<< " << foo << ";

}



print(int i)

{

  cout<< " << i << ";

}



use.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include "my.h"



int main()

{

   int foo=7;

   print(foo);
   print(99);

}





I`m confused because the exercise told to do just that.
Last edited on
Not sure if that's the source of error, btu it might be: In your .cpp file, in which you define your functions, you have to give some context.

For example:
.h:
1
2
3
4
5
...
class Foo {
  public:
    int bar();
};


.cpp:
1
2
3
4
5
#include "... .h"

int Foo::bar() { // basically you're saying, that this is class Foo's bar() function.
  ...
}


Last edited on
extern int foo; Here you are declare that integer foo will appear somewhere in global scope, but there is no such integer defined anywhere in your program.

Add int foo; in your my.cpp
Getting this error:

Documents/my.cpp:9:11: error: ISO C++ forbids declaration of ‘print_foo’ with no type [-fpermissive]
print_foo()
^
Documents/my.cpp: In function ‘int print_foo()’:
Documents/my.cpp:9:11: error: new declaration ‘int print_foo()’
Documents/my.h:7:6: error: ambiguates old declaration ‘void print_foo()’

^
Documents/my.cpp: At global scope:
Documents/my.cpp:19:12: error: ISO C++ forbids declaration of ‘print’ with no type [-fpermissive]
print(int i)
^
Documents/my.cpp: In function ‘int print(int)’:
Documents/my.cpp:19:12: error: new declaration ‘int print(int)’
Documents/my.h:8:6: error: ambiguates old declaration ‘void print(int)’
extern int foo;
^
Your return types don't match.
my.cpp :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#include "my.h"
#include <iostream>


using namespace std;



void print_foo()

{

 cout<< " << foo << ";

}



void print(int i)

{

  cout<< " << i << ";

}




my.h :

1
2
3
4
5
6
7
8
9
10
11
12
13
14

class C {

public:


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


};




use.cpp :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include "my.h"



int main()

{

   int foo=7;

   print_foo();
   print(99);

}




Output:

<< foo << << i <<

So? It works as written. What did you expected to see?
C' mon it should print 7 and 99. I think the header files are somehow related to the issue to get some benefit of them, but I don`t know what.
cout<< " << foo << ";Prints string " << foo << " Do you see quotes? That everything between them is of another color (at least here, however your IDE should do this too). You are telling program to output strings. There is no variables involved.

Remove quotes here.
Now my files are as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include "my.h"



int main()

{

   int foo=7;

   print_foo();
   print(99);

}





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29


#include "my.h"
#include <iostream>


using namespace std;



void C::print_foo()

{

 cout  << foo << '\n'; 

}



void C::print(int i)

{

  cout << i << '\n';

}




1
2
3
4
5
6
7
8
9
10
11
12
13
14

class C {

public:


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


};




I`m getting strange error:

Documents/my.cpp:9:6: error: ‘C’ has not been declared
void C::print_foo()
^
Documents/my.cpp:19:6: error: ‘C’ has not been declared
void C::print(int i)


I don`t understand them because I thought they were already included with my.h.
I don't understand why you have a class at all. I don't see the need for one here.

What does it even mean to declare a class member as "extern", anyway? I've never seen that before.
I don´t have class anymore but now I`m getting this error:

In function `print_foo()':
my.cpp:(.text+0x6): undefined reference to `foo'
collect2: error: ld returned 1 exit status

Header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 

#include "my.h"
#include <iostream>


using namespace std;




void print_foo()

{

 cout  << foo << '\n'; 

}



void print(int i)

{

  cout << i << '\n';

}



What's foo? Where's it defined? I can't see any definition of it in that file.
Last edited on
...And read my first post in this topic.
Look, if I add int foo; in my.cpp the output will be something like 0 and 99.

Foo is surely defined in "my.h" (see my earlier post) and is initialized to hold the value of 7.
I`m just doing what I was instructed to do in the exercise unless I have misunderstood the use of header files.
Last edited on
Foo is surely defined in "my.h" (see my earlier post) and is initialized to hold the value of 7.
Where? Content of your my.h as posted:
1
2
3
extern int foo;
void print_foo();
void print(int);
There is no definition anywhere.
This:
1
2
3
4
5
6
int main()
{
   int foo=7;
   print_foo();
   print(99);
}
Creates new local variable main::foo, print_foo() has no idea about.

If you are getting random values for foo, it means that you did not initialize it.
Either do
int foo = 7; in my.cpp or foo = 7; in main before calling print_foo().
use.cpp looks now like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include "my.h"



int main()

{

   foo=7;

   print_foo();
   print(99);

}



Getting this:

g++ Documents/my.cpp Documents/use.cpp -o fast
/tmp/ccCagA8O.o: In function `print_foo()':
my.cpp:(.text+0x6): undefined reference to `foo'
/tmp/ccfjnp08.o: In function `main':
use.cpp:(.text+0x6): undefined reference to `foo'
collect2: error: ld returned 1 exit status
Last edited on
Did you add int foo; to my.cpp? I bet not.
To elaborate - extern int foo; doesn't actually define foo, or cause it to be instantiated. It just declares that foo is a global that's defined somewhere else.

If you don't actually define it somewhere else, then you get that linker error.
Now I understand...

Thanks!
Topic archived. No new replies allowed.