Linking files together with headers

Hello

I've got problems linking my files together.
Take, I've got two files.
A main-file and a sub-file.

The sub-file contains a void function which needs to be used in the main-file.

main-file:
1
2
3
4
5
6
7
8
#include <iostream>
#include "subfile.h"

using namespace std;
int main() {
plus(5, 11);
cout << result <<endl;
}


sub-file:
1
2
3
void plus(int x, int y) {
result = x + y;
}


How can I do that?

Thanks for reading,
Niely
g++ -o output main.cpp sub-file.cpp
On line 6 in the main-file you are trying to print an undefined variable. The plus() function should return a value and you should assign that value to a variable and then print the variable.

main.cpp

1
2
3
4
5
6
7
8
#include <iostream>
#include "subfile.h"

using namespace std;
int main() {
  int result = plus(5, 11);
  cout << result <<endl;
}


subfile.h

1
2
3
4
int plus(int x, int y) {
  result = x + y;
  return result
}
Last edited on
Did your exact code. Got an error back:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
sub.h: In function ‘void plus(int, int)’:
sub.h:3:8: error: return-statement with a value, in function returning 'void' [-fpermissive]
 return result;
        ^
plFTP.cpp: In function ‘int main()’:
plFTP.cpp:6:3: error: reference to ‘plus’ is ambiguous
   plus(5, 11);
   ^
In file included from plFTP.cpp:2:0:
sub.h:1:6: note: candidates are: void plus(int, int)
 void plus(int x, int y) {
      ^
In file included from /usr/include/c++/4.8/string:48:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from plFTP.cpp:1:
/usr/include/c++/4.8/bits/stl_function.h:140:12: note:                 template<class _Tp> struct std::plus
     struct plus : public binary_function<_Tp, _Tp, _Tp>
            ^
plFTP.cpp:7:10: error: ‘result’ was not declared in this scope
   cout<< result;
I apologize. This just goes to show that you should always test your code before giving it to someone else to use.

main.cpp

1
2
3
4
5
6
7
#include <iostream>
#include "subfile.h"

int main() {
  int result = plus(5, 11);
  std::cout << result << std::endl;
}


subfile.h

1
2
3
4
int plus(int x, int y) {
  int result = x + y;
  return result;
}


Line 20 of your compiler errors is an example why you should not use using directives.
Last edited on
So with 'using namespace std' this isn't working?
Why not, is it possible with it as well a small other way around?
It is possible, but you shouldn't. Have a look at Namspace Rule #1:

http://www.gotw.ca/publications/migrating_to_namespaces.htm
Last edited on
I understand what the error cause was. :)
namespacing influenced the plus function, renaming that one fixed it.

Thanks all! :)
Hi,

Just a point:

Don't rename your functions to avoid the namespace clash, instead get rid of using namespace std; . Put std:: before each std thing.

There is lots written about that Google it :+)

Happy New year, hope all is well at your end :+D
Okay thanks! :)
Everything is fine now.

Thanks a lot for all your help.
Topic archived. No new replies allowed.