6 Replies - 327 Views - Last Post: 06 March 2018 - 02:42 PM Rate Topic: -----

#1 randomdue   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-October 17

Functions in multiple files

Posted 06 March 2018 - 01:47 PM

Hello!

I am trying to create a program that is transferring a value to a function only to have that function modify and then return that value. The program is very simple but the tricky part is that I am trying to spread the code over 3 different files, 1 header file and then 2 C++ source files. The header file is containing the decleariton of the function, the first source file contains the calling of the function and the last source file contains the modification aswell as the return value within the function.

There might be several reasons to why this error occurs but I thought I would try to ask you guys since I dont know what I am doing wrong.

The code on the header file is the following:
#ifndef G1_H_INCLUDED
#define G1_H_INCLUDED

int add(int x);

#endif // G1_H_INCLUDED




The code on the first C++ source file is the following:
#include <iostream>
#include "g1.h"
using namespace std;

int main()
{
    int a(5), b;
    b = add(a);
    cout << "b = " << b << endl;
}



And finally, the code of the second C++ source file is this:
#include <iostream>
#include "g1.h"
using namespace std;

int add(int x)
{
    return x+5;
};



The problem that I am having is that there is an error in the first C++ source file(the one with the main in it). Specifically
at
b = add(a);
. The error says that unfedined refrence to add(int). So basically what is happening is that the main part of the program in the first source file cant use the function when it is called. The only reason to remove the error was to put
int add(int x)
{
    return x+5;
};


in the header file, but that isnt correct according to the assignment that I am working on. Any advise to why I cant call the function in the main?

Is This A Good Question/Topic? 0
  • +

Replies To: Functions in multiple files

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14097
  • View blog
  • Posts: 56,497
  • Joined: 12-June 08

Re: Functions in multiple files

Posted 06 March 2018 - 01:51 PM

Classes are used to create an object, right? A contained snow ball of functions, parameters, etc.

When you want to use a function in a class you need to create an instance of the class and then call it.

In this case something like:

g1 foo;
int some_var = foo.add(3);

Was This Post Helpful? 0
  • +
  • -

#3 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 832
  • View blog
  • Posts: 3,354
  • Joined: 13-June 14

Re: Functions in multiple files

Posted 06 March 2018 - 01:54 PM

The "undefined reference" is a linker error, because the linker can't find the implementation of your function. How are you compiling your code? You likely need to also give the path to the file containing the implementation.

For future reference, please make sure to include error messages in their entirety and exactly how they appear (i.e. without any paraphrasing), as they contain useful information.

modi123_1, the OP doesn't seem to be using classes at all.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14097
  • View blog
  • Posts: 56,497
  • Joined: 12-June 08

Re: Functions in multiple files

Posted 06 March 2018 - 02:07 PM

@ndc - doh, sorry. That's what I get for multitasking more than last night's sleep is allowing. Haha :)
Was This Post Helpful? 0
  • +
  • -

#5 randomdue   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-October 17

Re: Functions in multiple files

Posted 06 March 2018 - 02:07 PM

View Postndc85430, on 06 March 2018 - 01:54 PM, said:

The "undefined reference" is a linker error, because the linker can't find the implementation of your function. How are you compiling your code? You likely need to also give the path to the file containing the implementation.

For future reference, please make sure to include error messages in their entirety and exactly how they appear (i.e. without any paraphrasing), as they contain useful information.

modi123_1, the OP doesn't seem to be using classes at all.


Im not sure what you mean by "How im compiling the code", i only saved all the files in the same folder on the desktop and then ran the program after I opend them all. When it comes to giving the path to the file containing the implementation, how do I do that exactly?

Also, the entire error message looks the following:

Undefined refrence to 'add(int)'
error: 1d returned 1 exit status
Was This Post Helpful? 0
  • +
  • -

#6 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 832
  • View blog
  • Posts: 3,354
  • Joined: 13-June 14

Re: Functions in multiple files

Posted 06 March 2018 - 02:41 PM

Post the full command you're using to compile. Again, that command line should contain the name of the file containing the function's implementation.

There's no need to quote the previous post in full. Please just press "Reply" in future.
Was This Post Helpful? 1
  • +
  • -

#7 randomdue   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-October 17

Re: Functions in multiple files

Posted 06 March 2018 - 02:42 PM

I have no managed to solve it thanks to a user named newbie on the forum stackerflow.com. Apparently all I needed to do is to change a few settings. If anyone else is using codeblocks and have the same problem that I am having, try doing this.

"project -> properties -> build targets -> Select the files related in the bottom right section."

Althought thanks everyone who tried to help me, I guess I just didnt know how to use the compiler correctly!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1